Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [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 | |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 17 | #include <getopt.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 21 | #include <string> |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 22 | #include <string_view> |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 23 | |
| 24 | #include <android-base/file.h> |
| 25 | #include <android-base/logging.h> |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 26 | #include <android-base/strings.h> |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 27 | |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 28 | #include "edify/expr.h" |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 29 | #include "otautil/error_code.h" |
| 30 | #include "otautil/paths.h" |
| 31 | #include "updater/blockimg.h" |
Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 32 | #include "updater/build_info.h" |
| 33 | #include "updater/dynamic_partitions.h" |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 34 | #include "updater/install.h" |
| 35 | #include "updater/simulator_runtime.h" |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 36 | #include "updater/updater.h" |
| 37 | |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 38 | using namespace std::string_literals; |
| 39 | |
| 40 | void Usage(std::string_view name) { |
| 41 | LOG(INFO) << "Usage: " << name << "[--oem_settings <oem_property_file>]" |
| 42 | << "[--skip_functions <skip_function_file>]" |
| 43 | << " --source <source_target_file>" |
| 44 | << " --ota_package <ota_package>"; |
| 45 | } |
| 46 | |
| 47 | Value* SimulatorPlaceHolderFn(const char* name, State* /* state */, |
| 48 | const std::vector<std::unique_ptr<Expr>>& /* argv */) { |
| 49 | LOG(INFO) << "Skip function " << name << " in host simulation"; |
| 50 | return StringValue("t"); |
| 51 | } |
| 52 | |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 53 | int main(int argc, char** argv) { |
| 54 | // Write the logs to stdout. |
| 55 | android::base::InitLogging(argv, &android::base::StderrLogger); |
| 56 | |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 57 | std::string oem_settings; |
| 58 | std::string skip_function_file; |
| 59 | std::string source_target_file; |
| 60 | std::string package_name; |
| 61 | |
| 62 | constexpr struct option OPTIONS[] = { |
| 63 | { "oem_settings", required_argument, nullptr, 0 }, |
| 64 | { "ota_package", required_argument, nullptr, 0 }, |
| 65 | { "skip_functions", required_argument, nullptr, 0 }, |
| 66 | { "source", required_argument, nullptr, 0 }, |
| 67 | { nullptr, 0, nullptr, 0 }, |
| 68 | }; |
| 69 | |
| 70 | int arg; |
| 71 | int option_index; |
| 72 | while ((arg = getopt_long(argc, argv, "", OPTIONS, &option_index)) != -1) { |
| 73 | if (arg != 0) { |
| 74 | LOG(ERROR) << "Invalid command argument"; |
| 75 | Usage(argv[0]); |
| 76 | return EXIT_FAILURE; |
| 77 | } |
| 78 | auto option_name = OPTIONS[option_index].name; |
| 79 | // The same oem property file used during OTA generation. It's needed for file_getprop() to |
| 80 | // return the correct value for the source build. |
| 81 | if (option_name == "oem_settings"s) { |
| 82 | oem_settings = optarg; |
| 83 | } else if (option_name == "skip_functions"s) { |
| 84 | skip_function_file = optarg; |
| 85 | } else if (option_name == "source"s) { |
| 86 | source_target_file = optarg; |
| 87 | } else if (option_name == "ota_package"s) { |
| 88 | package_name = optarg; |
| 89 | } else { |
| 90 | Usage(argv[0]); |
| 91 | return EXIT_FAILURE; |
| 92 | } |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 95 | if (source_target_file.empty() || package_name.empty()) { |
| 96 | Usage(argv[0]); |
| 97 | return EXIT_FAILURE; |
| 98 | } |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 99 | |
| 100 | // Configure edify's functions. |
| 101 | RegisterBuiltins(); |
| 102 | RegisterInstallFunctions(); |
| 103 | RegisterBlockImageFunctions(); |
Tianjie Xu | d118833 | 2019-05-24 16:08:45 -0700 | [diff] [blame] | 104 | RegisterDynamicPartitionsFunctions(); |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 105 | |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 106 | if (!skip_function_file.empty()) { |
| 107 | std::string content; |
| 108 | if (!android::base::ReadFileToString(skip_function_file, &content)) { |
| 109 | PLOG(ERROR) << "Failed to read " << skip_function_file; |
| 110 | return EXIT_FAILURE; |
| 111 | } |
| 112 | |
| 113 | auto lines = android::base::Split(content, "\n"); |
| 114 | for (const auto& line : lines) { |
| 115 | if (line.empty() || android::base::StartsWith(line, "#")) { |
| 116 | continue; |
| 117 | } |
| 118 | RegisterFunction(line, SimulatorPlaceHolderFn); |
| 119 | } |
| 120 | } |
| 121 | |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 122 | TemporaryFile temp_saved_source; |
| 123 | TemporaryFile temp_last_command; |
| 124 | TemporaryDir temp_stash_base; |
| 125 | |
| 126 | Paths::Get().set_cache_temp_source(temp_saved_source.path); |
| 127 | Paths::Get().set_last_command_file(temp_last_command.path); |
| 128 | Paths::Get().set_stash_directory_base(temp_stash_base.path); |
| 129 | |
| 130 | TemporaryFile cmd_pipe; |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 131 | TemporaryDir source_temp_dir; |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 132 | |
Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 133 | BuildInfo source_build_info(source_temp_dir.path); |
| 134 | if (!source_build_info.ParseTargetFile(source_target_file, false)) { |
| 135 | LOG(ERROR) << "Failed to parse the target file " << source_target_file; |
| 136 | return EXIT_FAILURE; |
| 137 | } |
| 138 | |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 139 | if (!oem_settings.empty()) { |
| 140 | CHECK_EQ(0, access(oem_settings.c_str(), R_OK)); |
| 141 | source_build_info.SetOemSettings(oem_settings); |
| 142 | } |
| 143 | |
Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 144 | Updater updater(std::make_unique<SimulatorRuntime>(&source_build_info)); |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 145 | if (!updater.Init(cmd_pipe.release(), package_name, false)) { |
Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 146 | return EXIT_FAILURE; |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | if (!updater.RunUpdate()) { |
Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 150 | return EXIT_FAILURE; |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | LOG(INFO) << "\nscript succeeded, result: " << updater.GetResult(); |
| 154 | |
| 155 | return 0; |
| 156 | } |