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