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; |
Tianjie Xu | 60b242c | 2019-07-30 16:48:52 -0700 | [diff] [blame] | 61 | std::string work_dir; |
| 62 | bool keep_images = false; |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 63 | |
| 64 | constexpr struct option OPTIONS[] = { |
Tianjie Xu | 60b242c | 2019-07-30 16:48:52 -0700 | [diff] [blame] | 65 | { "keep_images", no_argument, nullptr, 0 }, |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 66 | { "oem_settings", required_argument, nullptr, 0 }, |
| 67 | { "ota_package", required_argument, nullptr, 0 }, |
| 68 | { "skip_functions", required_argument, nullptr, 0 }, |
| 69 | { "source", required_argument, nullptr, 0 }, |
Tianjie Xu | 60b242c | 2019-07-30 16:48:52 -0700 | [diff] [blame] | 70 | { "work_dir", required_argument, nullptr, 0 }, |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 71 | { nullptr, 0, nullptr, 0 }, |
| 72 | }; |
| 73 | |
| 74 | int arg; |
| 75 | int option_index; |
| 76 | while ((arg = getopt_long(argc, argv, "", OPTIONS, &option_index)) != -1) { |
| 77 | if (arg != 0) { |
| 78 | LOG(ERROR) << "Invalid command argument"; |
| 79 | Usage(argv[0]); |
| 80 | return EXIT_FAILURE; |
| 81 | } |
| 82 | auto option_name = OPTIONS[option_index].name; |
| 83 | // The same oem property file used during OTA generation. It's needed for file_getprop() to |
| 84 | // return the correct value for the source build. |
| 85 | if (option_name == "oem_settings"s) { |
| 86 | oem_settings = optarg; |
| 87 | } else if (option_name == "skip_functions"s) { |
| 88 | skip_function_file = optarg; |
| 89 | } else if (option_name == "source"s) { |
| 90 | source_target_file = optarg; |
| 91 | } else if (option_name == "ota_package"s) { |
| 92 | package_name = optarg; |
Tianjie Xu | 60b242c | 2019-07-30 16:48:52 -0700 | [diff] [blame] | 93 | } else if (option_name == "keep_images"s) { |
| 94 | keep_images = true; |
| 95 | } else if (option_name == "work_dir"s) { |
| 96 | work_dir = optarg; |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 97 | } else { |
| 98 | Usage(argv[0]); |
| 99 | return EXIT_FAILURE; |
| 100 | } |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 103 | if (source_target_file.empty() || package_name.empty()) { |
| 104 | Usage(argv[0]); |
| 105 | return EXIT_FAILURE; |
| 106 | } |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 107 | |
| 108 | // Configure edify's functions. |
| 109 | RegisterBuiltins(); |
| 110 | RegisterInstallFunctions(); |
| 111 | RegisterBlockImageFunctions(); |
Tianjie Xu | d118833 | 2019-05-24 16:08:45 -0700 | [diff] [blame] | 112 | RegisterDynamicPartitionsFunctions(); |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 113 | |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 114 | if (!skip_function_file.empty()) { |
| 115 | std::string content; |
| 116 | if (!android::base::ReadFileToString(skip_function_file, &content)) { |
| 117 | PLOG(ERROR) << "Failed to read " << skip_function_file; |
| 118 | return EXIT_FAILURE; |
| 119 | } |
| 120 | |
| 121 | auto lines = android::base::Split(content, "\n"); |
| 122 | for (const auto& line : lines) { |
| 123 | if (line.empty() || android::base::StartsWith(line, "#")) { |
| 124 | continue; |
| 125 | } |
| 126 | RegisterFunction(line, SimulatorPlaceHolderFn); |
| 127 | } |
| 128 | } |
| 129 | |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 130 | TemporaryFile temp_saved_source; |
| 131 | TemporaryFile temp_last_command; |
| 132 | TemporaryDir temp_stash_base; |
| 133 | |
| 134 | Paths::Get().set_cache_temp_source(temp_saved_source.path); |
| 135 | Paths::Get().set_last_command_file(temp_last_command.path); |
| 136 | Paths::Get().set_stash_directory_base(temp_stash_base.path); |
| 137 | |
| 138 | TemporaryFile cmd_pipe; |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 139 | TemporaryDir source_temp_dir; |
Tianjie Xu | 60b242c | 2019-07-30 16:48:52 -0700 | [diff] [blame] | 140 | if (work_dir.empty()) { |
| 141 | work_dir = source_temp_dir.path; |
| 142 | } |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 143 | |
Tianjie Xu | 60b242c | 2019-07-30 16:48:52 -0700 | [diff] [blame] | 144 | BuildInfo source_build_info(work_dir, keep_images); |
Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 145 | if (!source_build_info.ParseTargetFile(source_target_file, false)) { |
| 146 | LOG(ERROR) << "Failed to parse the target file " << source_target_file; |
| 147 | return EXIT_FAILURE; |
| 148 | } |
| 149 | |
Tianjie Xu | 7efd233 | 2019-07-15 16:13:49 -0700 | [diff] [blame] | 150 | if (!oem_settings.empty()) { |
| 151 | CHECK_EQ(0, access(oem_settings.c_str(), R_OK)); |
| 152 | source_build_info.SetOemSettings(oem_settings); |
| 153 | } |
| 154 | |
Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 155 | Updater updater(std::make_unique<SimulatorRuntime>(&source_build_info)); |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 156 | if (!updater.Init(cmd_pipe.release(), package_name, false)) { |
Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 157 | return EXIT_FAILURE; |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | if (!updater.RunUpdate()) { |
Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -0700 | [diff] [blame] | 161 | return EXIT_FAILURE; |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | LOG(INFO) << "\nscript succeeded, result: " << updater.GetResult(); |
| 165 | |
| 166 | return 0; |
| 167 | } |