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