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 << "  --set-trending-issue-pattern <string within 2000 byte> Write a regex string";
61   std::cerr << "  --set-disable-faceauth-eval   Write disable-faceauth-eval flag\n";
62   std::cerr << "  --clear-disable-faceauth-eval Clear disable-faceauth-eval flag\n";
63   std::cerr << "Writes the given hex string to the specified offset in vendor space in /misc "
64                "partition.\nDefault offset is used for each action unless "
65                "--override-vendor-space-offset is specified.\n";
66   return EXIT_FAILURE;
67 }
68 
69 // misc_writer is a vendor tool that writes data to the vendor space in /misc.
main(int argc,char ** argv)70 int main(int argc, char** argv) {
71   constexpr struct option OPTIONS[] = {
72     { "set-dark-theme", no_argument, nullptr, 0 },
73     { "clear-dark-theme", no_argument, nullptr, 0 },
74     { "set-sota", no_argument, nullptr, 0 },
75     { "clear-sota", no_argument, nullptr, 0 },
76     { "set-wrist-orientation", required_argument, nullptr, 0 },
77     { "clear-wrist-orientation", no_argument, nullptr, 0 },
78     { "override-vendor-space-offset", required_argument, nullptr, 0 },
79     { "set-enable-pkvm", no_argument, nullptr, 0 },
80     { "set-disable-pkvm", no_argument, nullptr, 0 },
81     { "set-timeformat", required_argument, nullptr, 0},
82     { "set-timeoffset", required_argument, nullptr, 0},
83     { "set-max-ram-size", required_argument, nullptr, 0},
84     { "set-timertcoffset", required_argument, nullptr, 0},
85     { "set-minrtc", required_argument, nullptr, 0},
86     { "set-sota-config", no_argument, nullptr, 0 },
87     { "set-dsttransition", required_argument, nullptr, 0},
88     { "set-dstoffset", required_argument, nullptr, 0 },
89     { "set-display-mode", required_argument, nullptr, 0 },
90     { "clear-display-mode", no_argument, nullptr, 0 },
91     { "set-disable-faceauth-eval", no_argument, nullptr, 0 },
92     { "clear-disable-faceauth-eval", no_argument, nullptr, 0 },
93     { "set-trending-issue-pattern", required_argument, nullptr, 0 },
94     { nullptr, 0, nullptr, 0 },
95   };
96 
97   std::map<std::string, MiscWriterActions> action_map{
98     { "set-dark-theme", MiscWriterActions::kSetDarkThemeFlag },
99     { "clear-dark-theme", MiscWriterActions::kClearDarkThemeFlag },
100     { "set-sota", MiscWriterActions::kSetSotaFlag },
101     { "clear-sota", MiscWriterActions::kClearSotaFlag },
102     { "set-enable-pkvm", MiscWriterActions::kSetEnablePkvmFlag },
103     { "set-disable-pkvm", MiscWriterActions::kSetDisablePkvmFlag },
104     { "clear-wrist-orientation", MiscWriterActions::kClearWristOrientationFlag },
105     { "set-sota-config", MiscWriterActions::kSetSotaConfig },
106     { "clear-display-mode", MiscWriterActions::kClearDisplayMode },
107     { "set-disable-faceauth-eval", MiscWriterActions::kSetDisableFaceauthEval },
108     { "clear-disable-faceauth-eval", MiscWriterActions::kClearDisableFaceauthEval },
109   };
110 
111   std::unique_ptr<MiscWriter> misc_writer;
112   std::optional<size_t> override_offset;
113 
114   int arg;
115   int option_index = 0;
116   while ((arg = getopt_long(argc, argv, "", OPTIONS, &option_index)) != -1) {
117     if (arg != 0) {
118       LOG(ERROR) << "Invalid command argument";
119       return Usage(argv[0]);
120     }
121     auto option_name = OPTIONS[option_index].name;
122     if (option_name == "override-vendor-space-offset"s) {
123       LOG(WARNING) << "Overriding the vendor space offset in misc partition to " << optarg;
124       size_t offset;
125       if (!android::base::ParseUint(optarg, &offset)) {
126         LOG(ERROR) << "Failed to parse the offset: " << optarg;
127         return Usage(argv[0]);
128       }
129       override_offset = offset;
130     } else if (option_name == "set-wrist-orientation"s) {
131       int orientation;
132       if (!android::base::ParseInt(optarg, &orientation)) {
133         LOG(ERROR) << "Failed to parse the orientation: " << optarg;
134         return Usage(argv[0]);
135       }
136       if (orientation < 0 || orientation > 3) {
137         LOG(ERROR) << "Orientation out of range: " << optarg;
138         return Usage(argv[0]);
139       }
140       if (misc_writer) {
141         LOG(ERROR) << "Misc writer action has already been set";
142         return Usage(argv[0]);
143       }
144       misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kSetWristOrientationFlag,
145                                                      '0' + orientation);
146     } else if (option_name == "set-timeformat"s) {
147       int timeformat;
148       if (!android::base::ParseInt(optarg, &timeformat)) {
149         LOG(ERROR) << "Failed to parse the timeformat: " << optarg;
150         return Usage(argv[0]);
151       }
152       if (timeformat < 0 || timeformat > 1) {
153         LOG(ERROR) << "Time format out of range: " << optarg;
154         return Usage(argv[0]);
155       }
156       if (misc_writer) {
157         LOG(ERROR) << "Misc writer action has already been set";
158         return Usage(argv[0]);
159       }
160       misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kWriteTimeFormat,
161                                                      '0' + timeformat);
162     } else if (option_name == "set-timeoffset"s) {
163       int timeoffset;
164       if (!android::base::ParseInt(optarg, &timeoffset)) {
165         LOG(ERROR) << "Failed to parse the timeoffset: " << optarg;
166         return Usage(argv[0]);
167       }
168       if (timeoffset < MiscWriter::kMinTimeOffset || timeoffset > MiscWriter::kMaxTimeOffset) {
169         LOG(ERROR) << "Time offset out of range: " << optarg;
170         return Usage(argv[0]);
171       }
172       if (misc_writer) {
173         LOG(ERROR) << "Misc writer action has already been set";
174         return Usage(argv[0]);
175       }
176       misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kWriteTimeOffset,
177                                                      std::to_string(timeoffset));
178     } else if (option_name == "set-max-ram-size"s) {
179       int max_ram_size;
180       if (!android::base::ParseInt(optarg, &max_ram_size)) {
181         LOG(ERROR) << "Failed to parse the max_ram_size: " << optarg;
182         return Usage(argv[0]);
183       }
184       if (max_ram_size != MiscWriter::kRamSizeDefault &&
185           (max_ram_size < MiscWriter::kRamSizeMin || max_ram_size > MiscWriter::kRamSizeMax)) {
186         LOG(ERROR) << "max_ram_size out of range: " << optarg;
187         return Usage(argv[0]);
188       }
189       if (misc_writer) {
190         LOG(ERROR) << "Misc writer action has already been set";
191         return Usage(argv[0]);
192       }
193 
194       if (max_ram_size == MiscWriter::kRamSizeDefault) {
195         misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kClearMaxRamSize);
196       } else {
197         misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kSetMaxRamSize,
198                                                    std::to_string(max_ram_size));
199       }
200     } else if (option_name == "set-timertcoffset"s) {
201       long long int timertcoffset = strtoll(optarg, NULL, 10);
202       if (0 == timertcoffset) {
203         LOG(ERROR) << "Failed to parse the timertcoffset:" << optarg;
204         return Usage(argv[0]);
205       }
206       if (misc_writer) {
207         LOG(ERROR) << "Misc writer action has already been set";
208         return Usage(argv[0]);
209       }
210       misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kWriteTimeRtcOffset,
211                                                      std::to_string(timertcoffset));
212     } else if (option_name == "set-minrtc"s) {
213       long long int minrtc = strtoll(optarg, NULL, 10);
214       if (0 == minrtc) {
215         LOG(ERROR) << "Failed to parse the minrtc:" << optarg;
216         return Usage(argv[0]);
217       }
218       if (misc_writer) {
219         LOG(ERROR) << "Misc writer action has already been set";
220         return Usage(argv[0]);
221       }
222       misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kWriteTimeMinRtc,
223                                                      std::to_string(minrtc));
224     } else if (option_name == "set-display-mode"s) {
225       std::string mode(optarg);
226       if (mode.size() > MiscWriter::kDisplayModeMaxSize) {
227         LOG(ERROR) << "Display mode too long:" << optarg;
228         return Usage(argv[0]);
229       }
230       if (misc_writer) {
231         LOG(ERROR) << "Misc writer action has already been set";
232         return Usage(argv[0]);
233       }
234       misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kSetDisplayMode, mode);
235     } else if (auto iter = action_map.find(option_name); iter != action_map.end()) {
236       if (misc_writer) {
237         LOG(ERROR) << "Misc writer action has already been set";
238         return Usage(argv[0]);
239       }
240       misc_writer = std::make_unique<MiscWriter>(iter->second);
241     } else if (option_name == "set-dsttransition"s) {
242       long long int dst_transition = strtoll(optarg, NULL, 10);
243       if (misc_writer) {
244         LOG(ERROR) << "Misc writer action has already been set";
245         return Usage(argv[0]);
246       }
247       misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kWriteDstTransition,
248                                                      std::to_string(dst_transition));
249     } else if (option_name == "set-dstoffset"s) {
250       int dst_offset;
251       if (!android::base::ParseInt(optarg, &dst_offset)) {
252         LOG(ERROR) << "Failed to parse the dst offset: " << optarg;
253         return Usage(argv[0]);
254       }
255       if (misc_writer) {
256         LOG(ERROR) << "Misc writer action has already been set";
257         return Usage(argv[0]);
258       }
259       misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kWriteDstOffset,
260                                                      std::to_string(dst_offset));
261     } else if (option_name == "set-trending-issue-pattern"s) {
262       if (argc != 3) {
263         std::cerr << "Not the right amount of arguements, we expect 1 argument but were provide " << argc - 2;
264         return EXIT_FAILURE;
265       }
266       if (misc_writer) {
267         LOG(ERROR) << "Misc writer action has already been set";
268         return Usage(argv[0]);
269       } else if (sizeof(argv[2]) >= 32) {
270         std::cerr << "String is too large, we only take strings smaller than 32, but you provide " << sizeof(argv[2]);
271         return Usage(argv[0]);
272       }
273       misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kWriteEagleEyePatterns, argv[2]);
274     } else {
275       LOG(FATAL) << "Unreachable path, option_name: " << option_name;
276     }
277   }
278 
279   if (!misc_writer) {
280     LOG(ERROR) << "An action must be specified for misc writer";
281     return Usage(argv[0]);
282   }
283 
284   if (!misc_writer->PerformAction(override_offset)) {
285     return EXIT_FAILURE;
286   }
287 
288   return EXIT_SUCCESS;
289 }
290