1 /*
2 * Copyright (C) 2009 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 "updater/updater.h"
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <string>
25
26 #include <android-base/logging.h>
27 #include <android-base/strings.h>
28 #include <selinux/android.h>
29 #include <selinux/label.h>
30 #include <selinux/selinux.h>
31 #include <ziparchive/zip_archive.h>
32
33 #include "edify/expr.h"
34 #include "otafault/config.h"
35 #include "otautil/DirUtil.h"
36 #include "otautil/SysUtil.h"
37 #include "otautil/cache_location.h"
38 #include "otautil/error_code.h"
39 #include "updater/blockimg.h"
40 #include "updater/install.h"
41
42 // Generated by the makefile, this function defines the
43 // RegisterDeviceExtensions() function, which calls all the
44 // registration functions for device-specific extensions.
45 #include "register.inc"
46
47 // Where in the package we expect to find the edify script to execute.
48 // (Note it's "updateR-script", not the older "update-script".)
49 static constexpr const char* SCRIPT_NAME = "META-INF/com/google/android/updater-script";
50
51 extern bool have_eio_error;
52
53 struct selabel_handle *sehandle;
54
UpdaterLogger(android::base::LogId,android::base::LogSeverity,const char *,const char *,unsigned int,const char * message)55 static void UpdaterLogger(android::base::LogId /* id */, android::base::LogSeverity /* severity */,
56 const char* /* tag */, const char* /* file */, unsigned int /* line */,
57 const char* message) {
58 fprintf(stdout, "%s\n", message);
59 }
60
main(int argc,char ** argv)61 int main(int argc, char** argv) {
62 // Various things log information to stdout or stderr more or less
63 // at random (though we've tried to standardize on stdout). The
64 // log file makes more sense if buffering is turned off so things
65 // appear in the right order.
66 setbuf(stdout, nullptr);
67 setbuf(stderr, nullptr);
68
69 // We don't have logcat yet under recovery. Update logs will always be written to stdout
70 // (which is redirected to recovery.log).
71 android::base::InitLogging(argv, &UpdaterLogger);
72
73 if (argc != 4 && argc != 5) {
74 LOG(ERROR) << "unexpected number of arguments: " << argc;
75 return 1;
76 }
77
78 char* version = argv[1];
79 if ((version[0] != '1' && version[0] != '2' && version[0] != '3') || version[1] != '\0') {
80 // We support version 1, 2, or 3.
81 LOG(ERROR) << "wrong updater binary API; expected 1, 2, or 3; got " << argv[1];
82 return 2;
83 }
84
85 // Set up the pipe for sending commands back to the parent process.
86
87 int fd = atoi(argv[2]);
88 FILE* cmd_pipe = fdopen(fd, "wb");
89 setlinebuf(cmd_pipe);
90
91 // Extract the script from the package.
92
93 const char* package_filename = argv[3];
94 MemMapping map;
95 if (!map.MapFile(package_filename)) {
96 LOG(ERROR) << "failed to map package " << argv[3];
97 return 3;
98 }
99 ZipArchiveHandle za;
100 int open_err = OpenArchiveFromMemory(map.addr, map.length, argv[3], &za);
101 if (open_err != 0) {
102 LOG(ERROR) << "failed to open package " << argv[3] << ": " << ErrorCodeString(open_err);
103 CloseArchive(za);
104 return 3;
105 }
106
107 ZipString script_name(SCRIPT_NAME);
108 ZipEntry script_entry;
109 int find_err = FindEntry(za, script_name, &script_entry);
110 if (find_err != 0) {
111 LOG(ERROR) << "failed to find " << SCRIPT_NAME << " in " << package_filename << ": "
112 << ErrorCodeString(find_err);
113 CloseArchive(za);
114 return 4;
115 }
116
117 std::string script;
118 script.resize(script_entry.uncompressed_length);
119 int extract_err = ExtractToMemory(za, &script_entry, reinterpret_cast<uint8_t*>(&script[0]),
120 script_entry.uncompressed_length);
121 if (extract_err != 0) {
122 LOG(ERROR) << "failed to read script from package: " << ErrorCodeString(extract_err);
123 CloseArchive(za);
124 return 5;
125 }
126
127 // Configure edify's functions.
128
129 RegisterBuiltins();
130 RegisterInstallFunctions();
131 RegisterBlockImageFunctions();
132 RegisterDeviceExtensions();
133
134 // Parse the script.
135
136 std::unique_ptr<Expr> root;
137 int error_count = 0;
138 int error = parse_string(script.c_str(), &root, &error_count);
139 if (error != 0 || error_count > 0) {
140 LOG(ERROR) << error_count << " parse errors";
141 CloseArchive(za);
142 return 6;
143 }
144
145 sehandle = selinux_android_file_context_handle();
146 selinux_android_set_sehandle(sehandle);
147
148 if (!sehandle) {
149 fprintf(cmd_pipe, "ui_print Warning: No file_contexts\n");
150 }
151
152 // Evaluate the parsed script.
153
154 UpdaterInfo updater_info;
155 updater_info.cmd_pipe = cmd_pipe;
156 updater_info.package_zip = za;
157 updater_info.version = atoi(version);
158 updater_info.package_zip_addr = map.addr;
159 updater_info.package_zip_len = map.length;
160
161 State state(script, &updater_info);
162
163 if (argc == 5) {
164 if (strcmp(argv[4], "retry") == 0) {
165 state.is_retry = true;
166 } else {
167 printf("unexpected argument: %s", argv[4]);
168 }
169 }
170 ota_io_init(za, state.is_retry);
171
172 std::string result;
173 bool status = Evaluate(&state, root, &result);
174
175 if (have_eio_error) {
176 fprintf(cmd_pipe, "retry_update\n");
177 }
178
179 if (!status) {
180 if (state.errmsg.empty()) {
181 LOG(ERROR) << "script aborted (no error message)";
182 fprintf(cmd_pipe, "ui_print script aborted (no error message)\n");
183 } else {
184 LOG(ERROR) << "script aborted: " << state.errmsg;
185 const std::vector<std::string> lines = android::base::Split(state.errmsg, "\n");
186 for (const std::string& line : lines) {
187 // Parse the error code in abort message.
188 // Example: "E30: This package is for bullhead devices."
189 if (!line.empty() && line[0] == 'E') {
190 if (sscanf(line.c_str(), "E%d: ", &state.error_code) != 1) {
191 LOG(ERROR) << "Failed to parse error code: [" << line << "]";
192 }
193 }
194 fprintf(cmd_pipe, "ui_print %s\n", line.c_str());
195 }
196 }
197
198 // Installation has been aborted. Set the error code to kScriptExecutionFailure unless
199 // a more specific code has been set in errmsg.
200 if (state.error_code == kNoError) {
201 state.error_code = kScriptExecutionFailure;
202 }
203 fprintf(cmd_pipe, "log error: %d\n", state.error_code);
204 // Cause code should provide additional information about the abort.
205 if (state.cause_code != kNoCause) {
206 fprintf(cmd_pipe, "log cause: %d\n", state.cause_code);
207 if (state.cause_code == kPatchApplicationFailure) {
208 LOG(INFO) << "Patch application failed, retry update.";
209 fprintf(cmd_pipe, "retry_update\n");
210 }
211 }
212
213 if (updater_info.package_zip) {
214 CloseArchive(updater_info.package_zip);
215 }
216 return 7;
217 } else {
218 fprintf(cmd_pipe, "ui_print script succeeded: result was [%s]\n", result.c_str());
219 }
220
221 if (updater_info.package_zip) {
222 CloseArchive(updater_info.package_zip);
223 }
224
225 return 0;
226 }
227