Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 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 | |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 17 | #include "updater/updater.h" |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 18 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 21 | #include <fcntl.h> |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 22 | #include <string.h> |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 24 | |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 25 | #include <string> |
| 26 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 27 | #include <android-base/logging.h> |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 28 | #include <android-base/strings.h> |
Jeff Vander Stoep | e35926e | 2017-06-14 15:30:39 -0700 | [diff] [blame] | 29 | #include <selinux/android.h> |
Elliott Hughes | 4bbd5bf | 2016-04-01 18:24:39 -0700 | [diff] [blame] | 30 | #include <selinux/label.h> |
| 31 | #include <selinux/selinux.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 32 | #include <ziparchive/zip_archive.h> |
Elliott Hughes | 4bbd5bf | 2016-04-01 18:24:39 -0700 | [diff] [blame] | 33 | |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 34 | #include "edify/expr.h" |
Tao Bao | 17054c0 | 2018-05-03 22:41:23 -0700 | [diff] [blame] | 35 | #include "otautil/dirutil.h" |
Tao Bao | 1fc5bf3 | 2017-10-06 07:43:41 -0700 | [diff] [blame] | 36 | #include "otautil/error_code.h" |
Tao Bao | 17054c0 | 2018-05-03 22:41:23 -0700 | [diff] [blame] | 37 | #include "otautil/sysutil.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 38 | #include "updater/blockimg.h" |
Yifan Hong | 8ff84d7 | 2018-12-19 16:21:55 -0800 | [diff] [blame] | 39 | #include "updater/dynamic_partitions.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 40 | #include "updater/install.h" |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 41 | |
Doug Zongker | 2b0fdc6 | 2009-06-19 11:22:06 -0700 | [diff] [blame] | 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 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 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".) |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 49 | static constexpr const char* SCRIPT_NAME = "META-INF/com/google/android/updater-script"; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 50 | |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 51 | <<<<<<< HEAD |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 52 | #define SELINUX_CONTEXTS_ZIP "file_contexts" |
| 53 | #define SELINUX_CONTEXTS_TMP "/tmp/file_contexts" |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 54 | |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 55 | extern bool have_eio_error; |
| 56 | |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 57 | ======= |
| 58 | >>>>>>> android-10.0.0_r25 |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 59 | struct selabel_handle *sehandle; |
| 60 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 61 | static void UpdaterLogger(android::base::LogId /* id */, android::base::LogSeverity /* severity */, |
| 62 | const char* /* tag */, const char* /* file */, unsigned int /* line */, |
| 63 | const char* message) { |
| 64 | fprintf(stdout, "%s\n", message); |
| 65 | } |
| 66 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 67 | int main(int argc, char** argv) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 68 | // Various things log information to stdout or stderr more or less |
| 69 | // at random (though we've tried to standardize on stdout). The |
| 70 | // log file makes more sense if buffering is turned off so things |
| 71 | // appear in the right order. |
| 72 | setbuf(stdout, nullptr); |
| 73 | setbuf(stderr, nullptr); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 74 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 75 | // We don't have logcat yet under recovery. Update logs will always be written to stdout |
| 76 | // (which is redirected to recovery.log). |
| 77 | android::base::InitLogging(argv, &UpdaterLogger); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 78 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 79 | if (argc != 4 && argc != 5) { |
| 80 | LOG(ERROR) << "unexpected number of arguments: " << argc; |
| 81 | return 1; |
| 82 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 83 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 84 | char* version = argv[1]; |
| 85 | if ((version[0] != '1' && version[0] != '2' && version[0] != '3') || version[1] != '\0') { |
| 86 | // We support version 1, 2, or 3. |
| 87 | LOG(ERROR) << "wrong updater binary API; expected 1, 2, or 3; got " << argv[1]; |
| 88 | return 2; |
| 89 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 90 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 91 | // Set up the pipe for sending commands back to the parent process. |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 92 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 93 | int fd = atoi(argv[2]); |
| 94 | FILE* cmd_pipe = fdopen(fd, "wb"); |
| 95 | setlinebuf(cmd_pipe); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 96 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 97 | // Extract the script from the package. |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 98 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 99 | const char* package_filename = argv[3]; |
| 100 | MemMapping map; |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 101 | if (!map.MapFile(package_filename)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 102 | LOG(ERROR) << "failed to map package " << argv[3]; |
| 103 | return 3; |
| 104 | } |
| 105 | ZipArchiveHandle za; |
| 106 | int open_err = OpenArchiveFromMemory(map.addr, map.length, argv[3], &za); |
| 107 | if (open_err != 0) { |
| 108 | LOG(ERROR) << "failed to open package " << argv[3] << ": " << ErrorCodeString(open_err); |
| 109 | CloseArchive(za); |
| 110 | return 3; |
| 111 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 112 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 113 | ZipString script_name(SCRIPT_NAME); |
| 114 | ZipEntry script_entry; |
| 115 | int find_err = FindEntry(za, script_name, &script_entry); |
| 116 | if (find_err != 0) { |
| 117 | LOG(ERROR) << "failed to find " << SCRIPT_NAME << " in " << package_filename << ": " |
| 118 | << ErrorCodeString(find_err); |
| 119 | CloseArchive(za); |
| 120 | return 4; |
| 121 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 122 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 123 | std::string script; |
| 124 | script.resize(script_entry.uncompressed_length); |
| 125 | int extract_err = ExtractToMemory(za, &script_entry, reinterpret_cast<uint8_t*>(&script[0]), |
| 126 | script_entry.uncompressed_length); |
| 127 | if (extract_err != 0) { |
| 128 | LOG(ERROR) << "failed to read script from package: " << ErrorCodeString(extract_err); |
| 129 | CloseArchive(za); |
| 130 | return 5; |
| 131 | } |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 132 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 133 | // Configure edify's functions. |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 134 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 135 | RegisterBuiltins(); |
| 136 | RegisterInstallFunctions(); |
| 137 | RegisterBlockImageFunctions(); |
Yifan Hong | 8ff84d7 | 2018-12-19 16:21:55 -0800 | [diff] [blame] | 138 | RegisterDynamicPartitionsFunctions(); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 139 | RegisterDeviceExtensions(); |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 140 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 141 | // Parse the script. |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 142 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 143 | std::unique_ptr<Expr> root; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 144 | int error_count = 0; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 145 | int error = ParseString(script, &root, &error_count); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 146 | if (error != 0 || error_count > 0) { |
| 147 | LOG(ERROR) << error_count << " parse errors"; |
| 148 | CloseArchive(za); |
| 149 | return 6; |
| 150 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 151 | |
Jeff Vander Stoep | e35926e | 2017-06-14 15:30:39 -0700 | [diff] [blame] | 152 | sehandle = selinux_android_file_context_handle(); |
| 153 | selinux_android_set_sehandle(sehandle); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 154 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 155 | if (!sehandle) { |
| 156 | fprintf(cmd_pipe, "ui_print Warning: No file_contexts\n"); |
| 157 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 158 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 159 | // Evaluate the parsed script. |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 160 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 161 | UpdaterInfo updater_info; |
| 162 | updater_info.cmd_pipe = cmd_pipe; |
| 163 | updater_info.package_zip = za; |
| 164 | updater_info.version = atoi(version); |
| 165 | updater_info.package_zip_addr = map.addr; |
| 166 | updater_info.package_zip_len = map.length; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 167 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 168 | State state(script, &updater_info); |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 169 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 170 | if (argc == 5) { |
| 171 | if (strcmp(argv[4], "retry") == 0) { |
| 172 | state.is_retry = true; |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 173 | } else { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 174 | printf("unexpected argument: %s", argv[4]); |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 175 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 176 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 177 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 178 | if (access(SELINUX_CONTEXTS_TMP, R_OK) == 0) { |
| 179 | struct selinux_opt seopts[] = { |
| 180 | { SELABEL_OPT_PATH, SELINUX_CONTEXTS_TMP } |
| 181 | }; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 182 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 183 | sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1); |
| 184 | } else { |
| 185 | struct selinux_opt seopts[] = { |
| 186 | { SELABEL_OPT_PATH, "/file_contexts" } |
| 187 | }; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 188 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 189 | sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1); |
| 190 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 191 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 192 | std::string result; |
| 193 | bool status = Evaluate(&state, root, &result); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 194 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 195 | if (!status) { |
| 196 | if (state.errmsg.empty()) { |
| 197 | LOG(ERROR) << "script aborted (no error message)"; |
| 198 | fprintf(cmd_pipe, "ui_print script aborted (no error message)\n"); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 199 | } else { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 200 | LOG(ERROR) << "script aborted: " << state.errmsg; |
| 201 | const std::vector<std::string> lines = android::base::Split(state.errmsg, "\n"); |
| 202 | for (const std::string& line : lines) { |
| 203 | // Parse the error code in abort message. |
| 204 | // Example: "E30: This package is for bullhead devices." |
| 205 | if (!line.empty() && line[0] == 'E') { |
Mikhail Lappo | 17e6d3f | 2017-03-23 16:56:50 +0100 | [diff] [blame] | 206 | if (sscanf(line.c_str(), "E%d: ", &state.error_code) != 1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 207 | LOG(ERROR) << "Failed to parse error code: [" << line << "]"; |
| 208 | } |
| 209 | } |
| 210 | fprintf(cmd_pipe, "ui_print %s\n", line.c_str()); |
| 211 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Tianjie Xu | e0c8879 | 2017-05-02 16:07:18 -0700 | [diff] [blame] | 214 | // Installation has been aborted. Set the error code to kScriptExecutionFailure unless |
| 215 | // a more specific code has been set in errmsg. |
| 216 | if (state.error_code == kNoError) { |
| 217 | state.error_code = kScriptExecutionFailure; |
| 218 | } |
| 219 | fprintf(cmd_pipe, "log error: %d\n", state.error_code); |
| 220 | // Cause code should provide additional information about the abort. |
| 221 | if (state.cause_code != kNoCause) { |
| 222 | fprintf(cmd_pipe, "log cause: %d\n", state.cause_code); |
Tianjie Xu | 6957555 | 2017-05-16 15:51:46 -0700 | [diff] [blame] | 223 | if (state.cause_code == kPatchApplicationFailure) { |
| 224 | LOG(INFO) << "Patch application failed, retry update."; |
| 225 | fprintf(cmd_pipe, "retry_update\n"); |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 226 | } else if (state.cause_code == kEioFailure) { |
| 227 | LOG(INFO) << "Update failed due to EIO, retry update."; |
| 228 | fprintf(cmd_pipe, "retry_update\n"); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 229 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Doug Zongker | 8e5e4da | 2010-09-14 18:06:55 -0700 | [diff] [blame] | 232 | if (updater_info.package_zip) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 233 | CloseArchive(updater_info.package_zip); |
Doug Zongker | 8e5e4da | 2010-09-14 18:06:55 -0700 | [diff] [blame] | 234 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 235 | return 7; |
| 236 | } else { |
| 237 | fprintf(cmd_pipe, "ui_print script succeeded: result was [%s]\n", result.c_str()); |
| 238 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 239 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 240 | if (updater_info.package_zip) { |
| 241 | CloseArchive(updater_info.package_zip); |
| 242 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 243 | |
| 244 | return 0; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 245 | } |