blob: e87c57a5c8e445f754c4c0ef51b5af5b83eb262a [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
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
Tao Bao0c7839a2016-10-10 15:48:37 -070017#include "updater/updater.h"
Tao Bao59dcb9c2016-10-03 18:06:46 -070018
Doug Zongker9931f7f2009-06-10 14:11:53 -070019#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070020#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080021#include <string.h>
Tao Bao641fa972018-04-25 18:59:40 -070022#include <unistd.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070023
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070024#include <string>
25
Tao Bao039f2da2016-11-22 16:29:50 -080026#include <android-base/logging.h>
Tao Bao59dcb9c2016-10-03 18:06:46 -070027#include <android-base/strings.h>
Jeff Vander Stoepe35926e2017-06-14 15:30:39 -070028#include <selinux/android.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070029#include <selinux/label.h>
30#include <selinux/selinux.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070031#include <ziparchive/zip_archive.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070032
Tao Bao59dcb9c2016-10-03 18:06:46 -070033#include "edify/expr.h"
Tao Bao17054c02018-05-03 22:41:23 -070034#include "otautil/dirutil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070035#include "otautil/error_code.h"
Tao Bao17054c02018-05-03 22:41:23 -070036#include "otautil/sysutil.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070037#include "updater/blockimg.h"
38#include "updater/install.h"
Tao Bao59dcb9c2016-10-03 18:06:46 -070039
Doug Zongker2b0fdc62009-06-19 11:22:06 -070040// Generated by the makefile, this function defines the
41// RegisterDeviceExtensions() function, which calls all the
42// registration functions for device-specific extensions.
43#include "register.inc"
44
Doug Zongker9931f7f2009-06-10 14:11:53 -070045// Where in the package we expect to find the edify script to execute.
46// (Note it's "updateR-script", not the older "update-script".)
Tao Bao039f2da2016-11-22 16:29:50 -080047static constexpr const char* SCRIPT_NAME = "META-INF/com/google/android/updater-script";
Doug Zongker9931f7f2009-06-10 14:11:53 -070048
Stephen Smalley779701d2012-02-09 14:13:23 -050049struct selabel_handle *sehandle;
50
Tao Bao039f2da2016-11-22 16:29:50 -080051static void UpdaterLogger(android::base::LogId /* id */, android::base::LogSeverity /* severity */,
52 const char* /* tag */, const char* /* file */, unsigned int /* line */,
53 const char* message) {
54 fprintf(stdout, "%s\n", message);
55}
56
Doug Zongker9931f7f2009-06-10 14:11:53 -070057int main(int argc, char** argv) {
Tao Bao039f2da2016-11-22 16:29:50 -080058 // Various things log information to stdout or stderr more or less
59 // at random (though we've tried to standardize on stdout). The
60 // log file makes more sense if buffering is turned off so things
61 // appear in the right order.
62 setbuf(stdout, nullptr);
63 setbuf(stderr, nullptr);
Doug Zongker512536a2010-02-17 16:11:44 -080064
Tao Bao039f2da2016-11-22 16:29:50 -080065 // We don't have logcat yet under recovery. Update logs will always be written to stdout
66 // (which is redirected to recovery.log).
67 android::base::InitLogging(argv, &UpdaterLogger);
Doug Zongker9931f7f2009-06-10 14:11:53 -070068
Tao Bao039f2da2016-11-22 16:29:50 -080069 if (argc != 4 && argc != 5) {
70 LOG(ERROR) << "unexpected number of arguments: " << argc;
71 return 1;
72 }
Doug Zongker9931f7f2009-06-10 14:11:53 -070073
Tao Bao039f2da2016-11-22 16:29:50 -080074 char* version = argv[1];
75 if ((version[0] != '1' && version[0] != '2' && version[0] != '3') || version[1] != '\0') {
76 // We support version 1, 2, or 3.
77 LOG(ERROR) << "wrong updater binary API; expected 1, 2, or 3; got " << argv[1];
78 return 2;
79 }
Doug Zongker9931f7f2009-06-10 14:11:53 -070080
Tao Bao039f2da2016-11-22 16:29:50 -080081 // Set up the pipe for sending commands back to the parent process.
Doug Zongker9931f7f2009-06-10 14:11:53 -070082
Tao Bao039f2da2016-11-22 16:29:50 -080083 int fd = atoi(argv[2]);
84 FILE* cmd_pipe = fdopen(fd, "wb");
85 setlinebuf(cmd_pipe);
Doug Zongker9931f7f2009-06-10 14:11:53 -070086
Tao Bao039f2da2016-11-22 16:29:50 -080087 // Extract the script from the package.
Doug Zongker9931f7f2009-06-10 14:11:53 -070088
Tao Bao039f2da2016-11-22 16:29:50 -080089 const char* package_filename = argv[3];
90 MemMapping map;
Tao Baob656a152017-04-18 23:54:29 -070091 if (!map.MapFile(package_filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -080092 LOG(ERROR) << "failed to map package " << argv[3];
93 return 3;
94 }
95 ZipArchiveHandle za;
96 int open_err = OpenArchiveFromMemory(map.addr, map.length, argv[3], &za);
97 if (open_err != 0) {
98 LOG(ERROR) << "failed to open package " << argv[3] << ": " << ErrorCodeString(open_err);
99 CloseArchive(za);
100 return 3;
101 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700102
Tao Bao039f2da2016-11-22 16:29:50 -0800103 ZipString script_name(SCRIPT_NAME);
104 ZipEntry script_entry;
105 int find_err = FindEntry(za, script_name, &script_entry);
106 if (find_err != 0) {
107 LOG(ERROR) << "failed to find " << SCRIPT_NAME << " in " << package_filename << ": "
108 << ErrorCodeString(find_err);
109 CloseArchive(za);
110 return 4;
111 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700112
Tao Bao039f2da2016-11-22 16:29:50 -0800113 std::string script;
114 script.resize(script_entry.uncompressed_length);
115 int extract_err = ExtractToMemory(za, &script_entry, reinterpret_cast<uint8_t*>(&script[0]),
116 script_entry.uncompressed_length);
117 if (extract_err != 0) {
118 LOG(ERROR) << "failed to read script from package: " << ErrorCodeString(extract_err);
119 CloseArchive(za);
120 return 5;
121 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700122
Tao Bao039f2da2016-11-22 16:29:50 -0800123 // Configure edify's functions.
Doug Zongker9931f7f2009-06-10 14:11:53 -0700124
Tao Bao039f2da2016-11-22 16:29:50 -0800125 RegisterBuiltins();
126 RegisterInstallFunctions();
127 RegisterBlockImageFunctions();
128 RegisterDeviceExtensions();
Doug Zongker9931f7f2009-06-10 14:11:53 -0700129
Tao Bao039f2da2016-11-22 16:29:50 -0800130 // Parse the script.
Doug Zongker9931f7f2009-06-10 14:11:53 -0700131
Tianjie Xuc4447322017-03-06 14:44:59 -0800132 std::unique_ptr<Expr> root;
Tao Bao039f2da2016-11-22 16:29:50 -0800133 int error_count = 0;
Tao Baod8d514f2018-07-09 13:32:28 -0700134 int error = ParseString(script, &root, &error_count);
Tao Bao039f2da2016-11-22 16:29:50 -0800135 if (error != 0 || error_count > 0) {
136 LOG(ERROR) << error_count << " parse errors";
137 CloseArchive(za);
138 return 6;
139 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500140
Jeff Vander Stoepe35926e2017-06-14 15:30:39 -0700141 sehandle = selinux_android_file_context_handle();
142 selinux_android_set_sehandle(sehandle);
Stephen Smalley779701d2012-02-09 14:13:23 -0500143
Tao Bao039f2da2016-11-22 16:29:50 -0800144 if (!sehandle) {
145 fprintf(cmd_pipe, "ui_print Warning: No file_contexts\n");
146 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700147
Tao Bao039f2da2016-11-22 16:29:50 -0800148 // Evaluate the parsed script.
Doug Zongker9931f7f2009-06-10 14:11:53 -0700149
Tao Bao039f2da2016-11-22 16:29:50 -0800150 UpdaterInfo updater_info;
151 updater_info.cmd_pipe = cmd_pipe;
152 updater_info.package_zip = za;
153 updater_info.version = atoi(version);
154 updater_info.package_zip_addr = map.addr;
155 updater_info.package_zip_len = map.length;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700156
Tao Bao039f2da2016-11-22 16:29:50 -0800157 State state(script, &updater_info);
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700158
Tao Bao039f2da2016-11-22 16:29:50 -0800159 if (argc == 5) {
160 if (strcmp(argv[4], "retry") == 0) {
161 state.is_retry = true;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700162 } else {
Tao Bao039f2da2016-11-22 16:29:50 -0800163 printf("unexpected argument: %s", argv[4]);
164 }
165 }
166
167 std::string result;
168 bool status = Evaluate(&state, root, &result);
169
Tao Bao039f2da2016-11-22 16:29:50 -0800170 if (!status) {
171 if (state.errmsg.empty()) {
172 LOG(ERROR) << "script aborted (no error message)";
173 fprintf(cmd_pipe, "ui_print script aborted (no error message)\n");
174 } else {
175 LOG(ERROR) << "script aborted: " << state.errmsg;
176 const std::vector<std::string> lines = android::base::Split(state.errmsg, "\n");
177 for (const std::string& line : lines) {
178 // Parse the error code in abort message.
179 // Example: "E30: This package is for bullhead devices."
180 if (!line.empty() && line[0] == 'E') {
Mikhail Lappo17e6d3f2017-03-23 16:56:50 +0100181 if (sscanf(line.c_str(), "E%d: ", &state.error_code) != 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800182 LOG(ERROR) << "Failed to parse error code: [" << line << "]";
183 }
184 }
185 fprintf(cmd_pipe, "ui_print %s\n", line.c_str());
186 }
Tao Bao039f2da2016-11-22 16:29:50 -0800187 }
188
Tianjie Xue0c88792017-05-02 16:07:18 -0700189 // Installation has been aborted. Set the error code to kScriptExecutionFailure unless
190 // a more specific code has been set in errmsg.
191 if (state.error_code == kNoError) {
192 state.error_code = kScriptExecutionFailure;
193 }
194 fprintf(cmd_pipe, "log error: %d\n", state.error_code);
195 // Cause code should provide additional information about the abort.
196 if (state.cause_code != kNoCause) {
197 fprintf(cmd_pipe, "log cause: %d\n", state.cause_code);
Tianjie Xu69575552017-05-16 15:51:46 -0700198 if (state.cause_code == kPatchApplicationFailure) {
199 LOG(INFO) << "Patch application failed, retry update.";
200 fprintf(cmd_pipe, "retry_update\n");
Tianjie Xu22f11202018-08-27 10:50:31 -0700201 } else if (state.cause_code == kEioFailure) {
202 LOG(INFO) << "Update failed due to EIO, retry update.";
203 fprintf(cmd_pipe, "retry_update\n");
Tianjie Xu69575552017-05-16 15:51:46 -0700204 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700205 }
206
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700207 if (updater_info.package_zip) {
Tao Bao039f2da2016-11-22 16:29:50 -0800208 CloseArchive(updater_info.package_zip);
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700209 }
Tao Bao039f2da2016-11-22 16:29:50 -0800210 return 7;
211 } else {
212 fprintf(cmd_pipe, "ui_print script succeeded: result was [%s]\n", result.c_str());
213 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700214
Tao Bao039f2da2016-11-22 16:29:50 -0800215 if (updater_info.package_zip) {
216 CloseArchive(updater_info.package_zip);
217 }
Tao Bao039f2da2016-11-22 16:29:50 -0800218
219 return 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700220}