• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-enable-pkvm    Write the enable pKVM flag\n";
46   std::cerr << "  --set-disable-pkvm   Write the disable pKVM flag\n";
47   std::cerr << "Writes the given hex string to the specified offset in vendor space in /misc "
48                "partition.\nDefault offset is used for each action unless "
49                "--override-vendor-space-offset is specified.\n";
50   return EXIT_FAILURE;
51 }
52 
53 // misc_writer is a vendor tool that writes data to the vendor space in /misc.
main(int argc,char ** argv)54 int main(int argc, char** argv) {
55   constexpr struct option OPTIONS[] = {
56     { "set-dark-theme", no_argument, nullptr, 0 },
57     { "clear-dark-theme", no_argument, nullptr, 0 },
58     { "set-sota", no_argument, nullptr, 0 },
59     { "clear-sota", no_argument, nullptr, 0 },
60     { "override-vendor-space-offset", required_argument, nullptr, 0 },
61     { "set-enable-pkvm", no_argument, nullptr, 0 },
62     { "set-disable-pkvm", no_argument, nullptr, 0 },
63     { nullptr, 0, nullptr, 0 },
64   };
65 
66   std::map<std::string, MiscWriterActions> action_map{
67     { "set-dark-theme", MiscWriterActions::kSetDarkThemeFlag },
68     { "clear-dark-theme", MiscWriterActions::kClearDarkThemeFlag },
69     { "set-sota", MiscWriterActions::kSetSotaFlag },
70     { "clear-sota", MiscWriterActions::kClearSotaFlag },
71     { "set-enable-pkvm", MiscWriterActions::kSetEnablePkvmFlag },
72     { "set-disable-pkvm", MiscWriterActions::kSetDisablePkvmFlag },
73   };
74 
75   std::unique_ptr<MiscWriter> misc_writer;
76   std::optional<size_t> override_offset;
77 
78   int arg;
79   int option_index = 0;
80   while ((arg = getopt_long(argc, argv, "", OPTIONS, &option_index)) != -1) {
81     if (arg != 0) {
82       LOG(ERROR) << "Invalid command argument";
83       return Usage(argv[0]);
84     }
85     auto option_name = OPTIONS[option_index].name;
86     if (option_name == "override-vendor-space-offset"s) {
87       LOG(WARNING) << "Overriding the vendor space offset in misc partition to " << optarg;
88       size_t offset;
89       if (!android::base::ParseUint(optarg, &offset)) {
90         LOG(ERROR) << "Failed to parse the offset: " << optarg;
91         return Usage(argv[0]);
92       }
93       override_offset = offset;
94     } else if (auto iter = action_map.find(option_name); iter != action_map.end()) {
95       if (misc_writer) {
96         LOG(ERROR) << "Misc writer action has already been set";
97         return Usage(argv[0]);
98       }
99       misc_writer = std::make_unique<MiscWriter>(iter->second);
100     } else {
101       LOG(FATAL) << "Unreachable path, option_name: " << option_name;
102     }
103   }
104 
105   if (!misc_writer) {
106     LOG(ERROR) << "An action must be specified for misc writer";
107     return Usage(argv[0]);
108   }
109 
110   if (!misc_writer->PerformAction(override_offset)) {
111     return EXIT_FAILURE;
112   }
113 
114   return EXIT_SUCCESS;
115 }
116