blob: 278a440570463a921ed7cd7446cf1ff731ca44da [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;
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 Xuc1a5e262019-05-22 14:34:12 -070093 }
94
Tianjie Xu7efd2332019-07-15 16:13:49 -070095 if (source_target_file.empty() || package_name.empty()) {
96 Usage(argv[0]);
97 return EXIT_FAILURE;
98 }
Tianjie Xuc1a5e262019-05-22 14:34:12 -070099
100 // Configure edify's functions.
101 RegisterBuiltins();
102 RegisterInstallFunctions();
103 RegisterBlockImageFunctions();
Tianjie Xud1188332019-05-24 16:08:45 -0700104 RegisterDynamicPartitionsFunctions();
Tianjie Xuc1a5e262019-05-22 14:34:12 -0700105
Tianjie Xu7efd2332019-07-15 16:13:49 -0700106 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 Xuc1a5e262019-05-22 14:34:12 -0700122 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 Xuc1a5e262019-05-22 14:34:12 -0700131 TemporaryDir source_temp_dir;
Tianjie Xuc1a5e262019-05-22 14:34:12 -0700132
Tianjie Xu74b0f7c2019-05-22 13:59:57 -0700133 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 Xu7efd2332019-07-15 16:13:49 -0700139 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 Xu74b0f7c2019-05-22 13:59:57 -0700144 Updater updater(std::make_unique<SimulatorRuntime>(&source_build_info));
Tianjie Xuc1a5e262019-05-22 14:34:12 -0700145 if (!updater.Init(cmd_pipe.release(), package_name, false)) {
Tianjie Xu74b0f7c2019-05-22 13:59:57 -0700146 return EXIT_FAILURE;
Tianjie Xuc1a5e262019-05-22 14:34:12 -0700147 }
148
149 if (!updater.RunUpdate()) {
Tianjie Xu74b0f7c2019-05-22 13:59:57 -0700150 return EXIT_FAILURE;
Tianjie Xuc1a5e262019-05-22 14:34:12 -0700151 }
152
153 LOG(INFO) << "\nscript succeeded, result: " << updater.GetResult();
154
155 return 0;
156}