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 <stdlib.h>
19 #include <unistd.h>
20 
21 #include <string>
22 #include <string_view>
23 
24 #include <android-base/file.h>
25 #include <android-base/logging.h>
26 #include <android-base/strings.h>
27 
28 #include "edify/expr.h"
29 #include "otautil/error_code.h"
30 #include "otautil/paths.h"
31 #include "updater/blockimg.h"
32 #include "updater/build_info.h"
33 #include "updater/dynamic_partitions.h"
34 #include "updater/install.h"
35 #include "updater/simulator_runtime.h"
36 #include "updater/updater.h"
37 
38 using namespace std::string_literals;
39 
Usage(std::string_view name)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 
SimulatorPlaceHolderFn(const char * name,State *,const std::vector<std::unique_ptr<Expr>> &)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 
main(int argc,char ** argv)53 int main(int argc, char** argv) {
54   // Write the logs to stdout.
55   android::base::InitLogging(argv, &android::base::StderrLogger);
56 
57   std::string oem_settings;
58   std::string skip_function_file;
59   std::string source_target_file;
60   std::string package_name;
61   std::string work_dir;
62   bool keep_images = false;
63 
64   constexpr struct option OPTIONS[] = {
65     { "keep_images", no_argument, nullptr, 0 },
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 },
70     { "work_dir", required_argument, nullptr, 0 },
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;
93     } else if (option_name == "keep_images"s) {
94       keep_images = true;
95     } else if (option_name == "work_dir"s) {
96       work_dir = optarg;
97     } else {
98       Usage(argv[0]);
99       return EXIT_FAILURE;
100     }
101   }
102 
103   if (source_target_file.empty() || package_name.empty()) {
104     Usage(argv[0]);
105     return EXIT_FAILURE;
106   }
107 
108   // Configure edify's functions.
109   RegisterBuiltins();
110   RegisterInstallFunctions();
111   RegisterBlockImageFunctions();
112   RegisterDynamicPartitionsFunctions();
113 
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 
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;
139   TemporaryDir source_temp_dir;
140   if (work_dir.empty()) {
141     work_dir = source_temp_dir.path;
142   }
143 
144   BuildInfo source_build_info(work_dir, keep_images);
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 
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 
155   Updater updater(std::make_unique<SimulatorRuntime>(&source_build_info));
156   if (!updater.Init(cmd_pipe.release(), package_name, false)) {
157     return EXIT_FAILURE;
158   }
159 
160   if (!updater.RunUpdate()) {
161     return EXIT_FAILURE;
162   }
163 
164   LOG(INFO) << "\nscript succeeded, result: " << updater.GetResult();
165 
166   return 0;
167 }
168