blob: 6c6989bacbddd412eed18e599591b2e99953cd1a [file] [log] [blame]
Tianjie Xuc1a5e262019-05-22 14:34:12 -07001/*
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 Xu7efd2332019-07-15 16:13:49 -070017#include <getopt.h>
18#include <stdlib.h>
19#include <unistd.h>
20
Tianjie Xuc1a5e262019-05-22 14:34:12 -070021#include <string>
Tianjie Xu7efd2332019-07-15 16:13:49 -070022#include <string_view>
Tianjie Xuc1a5e262019-05-22 14:34:12 -070023
24#include <android-base/file.h>
25#include <android-base/logging.h>
Tianjie Xu7efd2332019-07-15 16:13:49 -070026#include <android-base/strings.h>
Tianjie Xuc1a5e262019-05-22 14:34:12 -070027
Tianjie Xu7efd2332019-07-15 16:13:49 -070028#include "edify/expr.h"
Tianjie Xuc1a5e262019-05-22 14:34:12 -070029#include "otautil/error_code.h"
30#include "otautil/paths.h"
31#include "updater/blockimg.h"
Tianjie Xu74b0f7c2019-05-22 13:59:57 -070032#include "updater/build_info.h"
33#include "updater/dynamic_partitions.h"
Tianjie Xuc1a5e262019-05-22 14:34:12 -070034#include "updater/install.h"
35#include "updater/simulator_runtime.h"
Tianjie Xuc1a5e262019-05-22 14:34:12 -070036#include "updater/updater.h"
37
Tianjie Xu7efd2332019-07-15 16:13:49 -070038using namespace std::string_literals;
39
40void 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
47Value* 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 Xuc1a5e262019-05-22 14:34:12 -070053int main(int argc, char** argv) {
54 // Write the logs to stdout.
55 android::base::InitLogging(argv, &android::base::StderrLogger);
56
Tianjie Xu7efd2332019-07-15 16:13:49 -070057 std::string oem_settings;
58 std::string skip_function_file;
59 std::string source_target_file;
60 std::string package_name;
Tianjie Xu60b242c2019-07-30 16:48:52 -070061 std::string work_dir;
62 bool keep_images = false;
Tianjie Xu7efd2332019-07-15 16:13:49 -070063
64 constexpr struct option OPTIONS[] = {
Tianjie Xu60b242c2019-07-30 16:48:52 -070065 { "keep_images", no_argument, nullptr, 0 },
Tianjie Xu7efd2332019-07-15 16:13:49 -070066 { "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 Xu60b242c2019-07-30 16:48:52 -070070 { "work_dir", required_argument, nullptr, 0 },
Tianjie Xu7efd2332019-07-15 16:13:49 -070071 { 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 Xu60b242c2019-07-30 16:48:52 -070093 } else if (option_name == "keep_images"s) {
94 keep_images = true;
95 } else if (option_name == "work_dir"s) {
96 work_dir = optarg;
Tianjie Xu7efd2332019-07-15 16:13:49 -070097 } else {
98 Usage(argv[0]);
99 return EXIT_FAILURE;
100 }
Tianjie Xuc1a5e262019-05-22 14:34:12 -0700101 }
102
Tianjie Xu7efd2332019-07-15 16:13:49 -0700103 if (source_target_file.empty() || package_name.empty()) {
104 Usage(argv[0]);
105 return EXIT_FAILURE;
106 }
Tianjie Xuc1a5e262019-05-22 14:34:12 -0700107
108 // Configure edify's functions.
109 RegisterBuiltins();
110 RegisterInstallFunctions();
111 RegisterBlockImageFunctions();
Tianjie Xud1188332019-05-24 16:08:45 -0700112 RegisterDynamicPartitionsFunctions();
Tianjie Xuc1a5e262019-05-22 14:34:12 -0700113
Tianjie Xu7efd2332019-07-15 16:13:49 -0700114 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 Xuc1a5e262019-05-22 14:34:12 -0700130 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 Xuc1a5e262019-05-22 14:34:12 -0700139 TemporaryDir source_temp_dir;
Tianjie Xu60b242c2019-07-30 16:48:52 -0700140 if (work_dir.empty()) {
141 work_dir = source_temp_dir.path;
142 }
Tianjie Xuc1a5e262019-05-22 14:34:12 -0700143
Tianjie Xu60b242c2019-07-30 16:48:52 -0700144 BuildInfo source_build_info(work_dir, keep_images);
Tianjie Xu74b0f7c2019-05-22 13:59:57 -0700145 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 Xu7efd2332019-07-15 16:13:49 -0700150 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 Xu74b0f7c2019-05-22 13:59:57 -0700155 Updater updater(std::make_unique<SimulatorRuntime>(&source_build_info));
Tianjie Xuc1a5e262019-05-22 14:34:12 -0700156 if (!updater.Init(cmd_pipe.release(), package_name, false)) {
Tianjie Xu74b0f7c2019-05-22 13:59:57 -0700157 return EXIT_FAILURE;
Tianjie Xuc1a5e262019-05-22 14:34:12 -0700158 }
159
160 if (!updater.RunUpdate()) {
Tianjie Xu74b0f7c2019-05-22 13:59:57 -0700161 return EXIT_FAILURE;
Tianjie Xuc1a5e262019-05-22 14:34:12 -0700162 }
163
164 LOG(INFO) << "\nscript succeeded, result: " << updater.GetResult();
165
166 return 0;
167}