blob: bd3b6feb3c2f9418ffa9e70ea65079d77cbbb926 [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>
20#include <unistd.h>
21#include <stdlib.h>
Dees_Troy512376c2013-09-03 19:39:41 +000022#include <fcntl.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080023#include <string.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070024
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070025#include <string>
26
Tao Bao039f2da2016-11-22 16:29:50 -080027#include <android-base/logging.h>
Tao Bao59dcb9c2016-10-03 18:06:46 -070028#include <android-base/strings.h>
Jeff Vander Stoepe35926e2017-06-14 15:30:39 -070029#include <selinux/android.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070030#include <selinux/label.h>
31#include <selinux/selinux.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070032#include <ziparchive/zip_archive.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070033
Tao Bao59dcb9c2016-10-03 18:06:46 -070034#include "edify/expr.h"
Tao Baod33b2f82017-09-28 21:29:11 -070035#include "otafault/config.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070036#include "otautil/DirUtil.h"
37#include "otautil/SysUtil.h"
Tianjie Xu3bbb20f2018-02-27 15:56:11 -080038#include "otautil/cache_location.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070039#include "otautil/error_code.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070040#include "updater/blockimg.h"
41#include "updater/install.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070042
Doug Zongker2b0fdc62009-06-19 11:22:06 -070043// Generated by the makefile, this function defines the
44// RegisterDeviceExtensions() function, which calls all the
45// registration functions for device-specific extensions.
46#include "register.inc"
47
Doug Zongker9931f7f2009-06-10 14:11:53 -070048// Where in the package we expect to find the edify script to execute.
49// (Note it's "updateR-script", not the older "update-script".)
Tao Bao039f2da2016-11-22 16:29:50 -080050static constexpr const char* SCRIPT_NAME = "META-INF/com/google/android/updater-script";
Doug Zongker9931f7f2009-06-10 14:11:53 -070051
Dees_Troy512376c2013-09-03 19:39:41 +000052#define SELINUX_CONTEXTS_ZIP "file_contexts"
53#define SELINUX_CONTEXTS_TMP "/tmp/file_contexts"
Doug Zongker9931f7f2009-06-10 14:11:53 -070054
Tianjie Xufa12b972016-02-05 18:25:58 -080055extern bool have_eio_error;
56
Stephen Smalley779701d2012-02-09 14:13:23 -050057struct selabel_handle *sehandle;
58
Tao Bao039f2da2016-11-22 16:29:50 -080059static void UpdaterLogger(android::base::LogId /* id */, android::base::LogSeverity /* severity */,
60 const char* /* tag */, const char* /* file */, unsigned int /* line */,
61 const char* message) {
62 fprintf(stdout, "%s\n", message);
63}
64
Doug Zongker9931f7f2009-06-10 14:11:53 -070065int main(int argc, char** argv) {
Tao Bao039f2da2016-11-22 16:29:50 -080066 // Various things log information to stdout or stderr more or less
67 // at random (though we've tried to standardize on stdout). The
68 // log file makes more sense if buffering is turned off so things
69 // appear in the right order.
70 setbuf(stdout, nullptr);
71 setbuf(stderr, nullptr);
Doug Zongker512536a2010-02-17 16:11:44 -080072
Tao Bao039f2da2016-11-22 16:29:50 -080073 // We don't have logcat yet under recovery. Update logs will always be written to stdout
74 // (which is redirected to recovery.log).
75 android::base::InitLogging(argv, &UpdaterLogger);
Doug Zongker9931f7f2009-06-10 14:11:53 -070076
Tao Bao039f2da2016-11-22 16:29:50 -080077 if (argc != 4 && argc != 5) {
78 LOG(ERROR) << "unexpected number of arguments: " << argc;
79 return 1;
80 }
Doug Zongker9931f7f2009-06-10 14:11:53 -070081
Tao Bao039f2da2016-11-22 16:29:50 -080082 char* version = argv[1];
83 if ((version[0] != '1' && version[0] != '2' && version[0] != '3') || version[1] != '\0') {
84 // We support version 1, 2, or 3.
85 LOG(ERROR) << "wrong updater binary API; expected 1, 2, or 3; got " << argv[1];
86 return 2;
87 }
Doug Zongker9931f7f2009-06-10 14:11:53 -070088
Tao Bao039f2da2016-11-22 16:29:50 -080089 // Set up the pipe for sending commands back to the parent process.
Doug Zongker9931f7f2009-06-10 14:11:53 -070090
Tao Bao039f2da2016-11-22 16:29:50 -080091 int fd = atoi(argv[2]);
92 FILE* cmd_pipe = fdopen(fd, "wb");
93 setlinebuf(cmd_pipe);
Doug Zongker9931f7f2009-06-10 14:11:53 -070094
Tao Bao039f2da2016-11-22 16:29:50 -080095 // Extract the script from the package.
Doug Zongker9931f7f2009-06-10 14:11:53 -070096
Tao Bao039f2da2016-11-22 16:29:50 -080097 const char* package_filename = argv[3];
98 MemMapping map;
Tao Baob656a152017-04-18 23:54:29 -070099 if (!map.MapFile(package_filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800100 LOG(ERROR) << "failed to map package " << argv[3];
101 return 3;
102 }
103 ZipArchiveHandle za;
104 int open_err = OpenArchiveFromMemory(map.addr, map.length, argv[3], &za);
105 if (open_err != 0) {
106 LOG(ERROR) << "failed to open package " << argv[3] << ": " << ErrorCodeString(open_err);
107 CloseArchive(za);
108 return 3;
109 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700110
Tao Bao039f2da2016-11-22 16:29:50 -0800111 ZipString script_name(SCRIPT_NAME);
112 ZipEntry script_entry;
113 int find_err = FindEntry(za, script_name, &script_entry);
114 if (find_err != 0) {
115 LOG(ERROR) << "failed to find " << SCRIPT_NAME << " in " << package_filename << ": "
116 << ErrorCodeString(find_err);
117 CloseArchive(za);
118 return 4;
119 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700120
Tao Bao039f2da2016-11-22 16:29:50 -0800121 std::string script;
122 script.resize(script_entry.uncompressed_length);
123 int extract_err = ExtractToMemory(za, &script_entry, reinterpret_cast<uint8_t*>(&script[0]),
124 script_entry.uncompressed_length);
125 if (extract_err != 0) {
126 LOG(ERROR) << "failed to read script from package: " << ErrorCodeString(extract_err);
127 CloseArchive(za);
128 return 5;
129 }
Dees_Troy512376c2013-09-03 19:39:41 +0000130
Tao Bao039f2da2016-11-22 16:29:50 -0800131 // Configure edify's functions.
Dees_Troy512376c2013-09-03 19:39:41 +0000132
Tao Bao039f2da2016-11-22 16:29:50 -0800133 RegisterBuiltins();
134 RegisterInstallFunctions();
135 RegisterBlockImageFunctions();
136 RegisterDeviceExtensions();
Dees_Troy512376c2013-09-03 19:39:41 +0000137
Tao Bao039f2da2016-11-22 16:29:50 -0800138 // Parse the script.
Doug Zongker9931f7f2009-06-10 14:11:53 -0700139
Tianjie Xuc4447322017-03-06 14:44:59 -0800140 std::unique_ptr<Expr> root;
Tao Bao039f2da2016-11-22 16:29:50 -0800141 int error_count = 0;
142 int error = parse_string(script.c_str(), &root, &error_count);
143 if (error != 0 || error_count > 0) {
144 LOG(ERROR) << error_count << " parse errors";
145 CloseArchive(za);
146 return 6;
147 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700148
Jeff Vander Stoepe35926e2017-06-14 15:30:39 -0700149 sehandle = selinux_android_file_context_handle();
150 selinux_android_set_sehandle(sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700151
Tao Bao039f2da2016-11-22 16:29:50 -0800152 if (!sehandle) {
153 fprintf(cmd_pipe, "ui_print Warning: No file_contexts\n");
154 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700155
Tao Bao039f2da2016-11-22 16:29:50 -0800156 // Evaluate the parsed script.
Stephen Smalley779701d2012-02-09 14:13:23 -0500157
Tao Bao039f2da2016-11-22 16:29:50 -0800158 UpdaterInfo updater_info;
159 updater_info.cmd_pipe = cmd_pipe;
160 updater_info.package_zip = za;
161 updater_info.version = atoi(version);
162 updater_info.package_zip_addr = map.addr;
163 updater_info.package_zip_len = map.length;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700164
Tao Bao039f2da2016-11-22 16:29:50 -0800165 State state(script, &updater_info);
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700166
Tao Bao039f2da2016-11-22 16:29:50 -0800167 if (argc == 5) {
168 if (strcmp(argv[4], "retry") == 0) {
169 state.is_retry = true;
Dees_Troy512376c2013-09-03 19:39:41 +0000170 } else {
Tao Bao039f2da2016-11-22 16:29:50 -0800171 printf("unexpected argument: %s", argv[4]);
Dees_Troy512376c2013-09-03 19:39:41 +0000172 }
Tao Bao039f2da2016-11-22 16:29:50 -0800173 }
Tianjie Xu47282422017-01-09 13:45:37 -0800174 ota_io_init(za, state.is_retry);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700175
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500176 if (access(SELINUX_CONTEXTS_TMP, R_OK) == 0) {
177 struct selinux_opt seopts[] = {
178 { SELABEL_OPT_PATH, SELINUX_CONTEXTS_TMP }
179 };
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700180
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500181 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
182 } else {
183 struct selinux_opt seopts[] = {
184 { SELABEL_OPT_PATH, "/file_contexts" }
185 };
Doug Zongker9931f7f2009-06-10 14:11:53 -0700186
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500187 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
188 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700189
Tao Bao039f2da2016-11-22 16:29:50 -0800190 std::string result;
191 bool status = Evaluate(&state, root, &result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700192
Tao Bao039f2da2016-11-22 16:29:50 -0800193 if (have_eio_error) {
194 fprintf(cmd_pipe, "retry_update\n");
195 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700196
Tao Bao039f2da2016-11-22 16:29:50 -0800197 if (!status) {
198 if (state.errmsg.empty()) {
199 LOG(ERROR) << "script aborted (no error message)";
200 fprintf(cmd_pipe, "ui_print script aborted (no error message)\n");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700201 } else {
Tao Bao039f2da2016-11-22 16:29:50 -0800202 LOG(ERROR) << "script aborted: " << state.errmsg;
203 const std::vector<std::string> lines = android::base::Split(state.errmsg, "\n");
204 for (const std::string& line : lines) {
205 // Parse the error code in abort message.
206 // Example: "E30: This package is for bullhead devices."
207 if (!line.empty() && line[0] == 'E') {
Mikhail Lappo17e6d3f2017-03-23 16:56:50 +0100208 if (sscanf(line.c_str(), "E%d: ", &state.error_code) != 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800209 LOG(ERROR) << "Failed to parse error code: [" << line << "]";
210 }
211 }
212 fprintf(cmd_pipe, "ui_print %s\n", line.c_str());
213 }
Tao Bao039f2da2016-11-22 16:29:50 -0800214 }
215
Tianjie Xue0c88792017-05-02 16:07:18 -0700216 // Installation has been aborted. Set the error code to kScriptExecutionFailure unless
217 // a more specific code has been set in errmsg.
218 if (state.error_code == kNoError) {
219 state.error_code = kScriptExecutionFailure;
220 }
221 fprintf(cmd_pipe, "log error: %d\n", state.error_code);
222 // Cause code should provide additional information about the abort.
223 if (state.cause_code != kNoCause) {
224 fprintf(cmd_pipe, "log cause: %d\n", state.cause_code);
Tianjie Xu69575552017-05-16 15:51:46 -0700225 if (state.cause_code == kPatchApplicationFailure) {
226 LOG(INFO) << "Patch application failed, retry update.";
227 fprintf(cmd_pipe, "retry_update\n");
Tao Bao039f2da2016-11-22 16:29:50 -0800228 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700229 }
230
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700231 if (updater_info.package_zip) {
Tao Bao039f2da2016-11-22 16:29:50 -0800232 CloseArchive(updater_info.package_zip);
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700233 }
Tao Bao039f2da2016-11-22 16:29:50 -0800234 return 7;
235 } else {
236 fprintf(cmd_pipe, "ui_print script succeeded: result was [%s]\n", result.c_str());
237 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700238
Tao Bao039f2da2016-11-22 16:29:50 -0800239 if (updater_info.package_zip) {
240 CloseArchive(updater_info.package_zip);
241 }
Tao Bao039f2da2016-11-22 16:29:50 -0800242
243 return 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700244}