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/install.h" |
| 18 | |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 19 | #include <ctype.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 20 | #include <errno.h> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
| 22 | #include <ftw.h> |
| 23 | #include <inttypes.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 24 | #include <stdarg.h> |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 25 | #include <stdio.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 28 | #include <sys/capability.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 29 | #include <sys/mount.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 32 | #include <sys/wait.h> |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 33 | #include <sys/xattr.h> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 34 | #include <time.h> |
| 35 | #include <unistd.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 36 | #include <utime.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 37 | |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 38 | #include <memory> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 39 | #include <string> |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 40 | #include <vector> |
| 41 | |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 42 | #include <android-base/file.h> |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 43 | #include <android-base/logging.h> |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 44 | #include <android-base/parsedouble.h> |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 45 | #include <android-base/parseint.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 46 | #include <android-base/properties.h> |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 47 | #include <android-base/stringprintf.h> |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 48 | #include <android-base/strings.h> |
Tao Bao | 0d3f84f | 2016-12-28 15:09:20 -0800 | [diff] [blame] | 49 | #include <applypatch/applypatch.h> |
| 50 | #include <bootloader_message/bootloader_message.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 51 | #include <cutils/android_reboot.h> |
Tao Bao | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 52 | #include <ext4_utils/wipe.h> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 53 | #include <openssl/sha.h> |
Elliott Hughes | 4bbd5bf | 2016-04-01 18:24:39 -0700 | [diff] [blame] | 54 | #include <selinux/label.h> |
| 55 | #include <selinux/selinux.h> |
Tao Bao | 09e468f | 2017-09-29 14:39:33 -0700 | [diff] [blame] | 56 | #include <tune2fs.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 57 | #include <ziparchive/zip_archive.h> |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 58 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 59 | #include "edify/expr.h" |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 60 | #include "mounts.h" |
Tao Bao | d33b2f8 | 2017-09-28 21:29:11 -0700 | [diff] [blame] | 61 | #include "otafault/ota_io.h" |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 62 | #include "otautil/DirUtil.h" |
Tao Bao | 1fc5bf3 | 2017-10-06 07:43:41 -0700 | [diff] [blame] | 63 | #include "otautil/error_code.h" |
Tao Bao | 09e468f | 2017-09-29 14:39:33 -0700 | [diff] [blame] | 64 | #include "otautil/print_sha1.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 65 | #include "updater/updater.h" |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 66 | |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 67 | // Send over the buffer to recovery though the command pipe. |
| 68 | static void uiPrint(State* state, const std::string& buffer) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 69 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 70 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 71 | // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "". |
| 72 | // So skip sending empty strings to UI. |
| 73 | std::vector<std::string> lines = android::base::Split(buffer, "\n"); |
| 74 | for (auto& line : lines) { |
| 75 | if (!line.empty()) { |
| 76 | fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str()); |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 77 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 78 | } |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 79 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 80 | // On the updater side, we need to dump the contents to stderr (which has |
| 81 | // been redirected to the log file). Because the recovery will only print |
| 82 | // the contents to screen when processing pipe command ui_print. |
| 83 | LOG(INFO) << buffer; |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Elliott Hughes | 83ce755 | 2016-06-30 09:28:42 -0700 | [diff] [blame] | 86 | void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 87 | std::string error_msg; |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 88 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 89 | va_list ap; |
| 90 | va_start(ap, format); |
| 91 | android::base::StringAppendV(&error_msg, format, ap); |
| 92 | va_end(ap); |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 93 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 94 | uiPrint(state, error_msg); |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 97 | // mount(fs_type, partition_type, location, mount_point) |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 98 | // mount(fs_type, partition_type, location, mount_point, mount_options) |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 99 | |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 100 | // fs_type="ext4" partition_type="EMMC" location=device |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 101 | Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 102 | if (argv.size() != 4 && argv.size() != 5) { |
| 103 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name, |
| 104 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 108 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 109 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 110 | } |
| 111 | const std::string& fs_type = args[0]; |
| 112 | const std::string& partition_type = args[1]; |
| 113 | const std::string& location = args[2]; |
| 114 | const std::string& mount_point = args[3]; |
| 115 | std::string mount_options; |
| 116 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 117 | if (argv.size() == 5) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 118 | mount_options = args[4]; |
| 119 | } |
| 120 | |
| 121 | if (fs_type.empty()) { |
| 122 | return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name); |
| 123 | } |
| 124 | if (partition_type.empty()) { |
| 125 | return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty", |
| 126 | name); |
| 127 | } |
| 128 | if (location.empty()) { |
| 129 | return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name); |
| 130 | } |
| 131 | if (mount_point.empty()) { |
| 132 | return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty", |
| 133 | name); |
| 134 | } |
| 135 | |
| 136 | { |
| 137 | char* secontext = nullptr; |
| 138 | |
| 139 | if (sehandle) { |
| 140 | selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755); |
| 141 | setfscreatecon(secontext); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 142 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 143 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 144 | mkdir(mount_point.c_str(), 0755); |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 145 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 146 | if (secontext) { |
| 147 | freecon(secontext); |
| 148 | setfscreatecon(nullptr); |
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 | } |
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 | if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(), |
| 153 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) { |
Tao Bao | 0bbc764 | 2017-03-29 23:57:47 -0700 | [diff] [blame] | 154 | uiPrintf(state, "%s: Failed to mount %s at %s: %s", name, location.c_str(), mount_point.c_str(), |
| 155 | strerror(errno)); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 156 | return StringValue(""); |
| 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 | return StringValue(mount_point); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 162 | // is_mounted(mount_point) |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 163 | Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 164 | if (argv.size() != 1) { |
| 165 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 166 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 167 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 168 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 169 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 170 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 171 | } |
| 172 | const std::string& mount_point = args[0]; |
| 173 | if (mount_point.empty()) { |
| 174 | return ErrorAbort(state, kArgsParsingFailure, |
| 175 | "mount_point argument to unmount() can't be empty"); |
| 176 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 177 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 178 | scan_mounted_volumes(); |
| 179 | MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str()); |
| 180 | if (vol == nullptr) { |
| 181 | return StringValue(""); |
| 182 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 183 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 184 | return StringValue(mount_point); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 187 | Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 188 | if (argv.size() != 1) { |
| 189 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 190 | } |
| 191 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 192 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 193 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 194 | } |
| 195 | const std::string& mount_point = args[0]; |
| 196 | if (mount_point.empty()) { |
| 197 | return ErrorAbort(state, kArgsParsingFailure, |
| 198 | "mount_point argument to unmount() can't be empty"); |
| 199 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 200 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 201 | scan_mounted_volumes(); |
| 202 | MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str()); |
| 203 | if (vol == nullptr) { |
Tao Bao | 0bbc764 | 2017-03-29 23:57:47 -0700 | [diff] [blame] | 204 | uiPrintf(state, "Failed to unmount %s: No such volume", mount_point.c_str()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 205 | return nullptr; |
| 206 | } else { |
| 207 | int ret = unmount_mounted_volume(vol); |
| 208 | if (ret != 0) { |
Tao Bao | 0bbc764 | 2017-03-29 23:57:47 -0700 | [diff] [blame] | 209 | uiPrintf(state, "Failed to unmount %s: %s", mount_point.c_str(), strerror(errno)); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 210 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 211 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 212 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 213 | return StringValue(mount_point); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 214 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 215 | |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 216 | static int exec_cmd(const char* path, char* const argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 217 | pid_t child; |
| 218 | if ((child = vfork()) == 0) { |
| 219 | execv(path, argv); |
Tao Bao | 3da8801 | 2017-02-03 13:09:23 -0800 | [diff] [blame] | 220 | _exit(EXIT_FAILURE); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | int status; |
| 224 | waitpid(child, &status, 0); |
| 225 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 226 | LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status); |
| 227 | } |
| 228 | return WEXITSTATUS(status); |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 231 | // format(fs_type, partition_type, location, fs_size, mount_point) |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 232 | // |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 233 | // fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location> |
| 234 | // fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location> |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 235 | // if fs_size == 0, then make fs uses the entire partition. |
Ken Sumrall | 8f132ed | 2011-01-14 18:55:05 -0800 | [diff] [blame] | 236 | // if fs_size > 0, that is the size to use |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 237 | // if fs_size < 0, then reserve that many bytes at the end of the partition (not for "f2fs") |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 238 | Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 239 | if (argv.size() != 5) { |
| 240 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name, |
| 241 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 242 | } |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 243 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 244 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 245 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 246 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 247 | } |
| 248 | const std::string& fs_type = args[0]; |
| 249 | const std::string& partition_type = args[1]; |
| 250 | const std::string& location = args[2]; |
| 251 | const std::string& fs_size = args[3]; |
| 252 | const std::string& mount_point = args[4]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 253 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 254 | if (fs_type.empty()) { |
| 255 | return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name); |
| 256 | } |
| 257 | if (partition_type.empty()) { |
| 258 | return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty", |
| 259 | name); |
| 260 | } |
| 261 | if (location.empty()) { |
| 262 | return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name); |
| 263 | } |
| 264 | if (mount_point.empty()) { |
| 265 | return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty", |
| 266 | name); |
| 267 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 268 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 269 | int64_t size; |
| 270 | if (!android::base::ParseInt(fs_size, &size)) { |
| 271 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name, |
| 272 | fs_size.c_str()); |
| 273 | } |
| 274 | |
| 275 | if (fs_type == "ext4") { |
Jin Qian | ded2dac | 2017-04-21 14:36:12 -0700 | [diff] [blame] | 276 | const char* mke2fs_argv[] = { "/sbin/mke2fs_static", "-t", "ext4", "-b", "4096", |
| 277 | location.c_str(), nullptr, nullptr }; |
| 278 | std::string size_str; |
| 279 | if (size != 0) { |
| 280 | size_str = std::to_string(size / 4096LL); |
| 281 | mke2fs_argv[6] = size_str.c_str(); |
| 282 | } |
| 283 | |
| 284 | int status = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv)); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 285 | if (status != 0) { |
Jin Qian | 502fd1c | 2017-11-02 11:58:12 -0700 | [diff] [blame] | 286 | LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location; |
| 287 | return StringValue(""); |
Jin Qian | ded2dac | 2017-04-21 14:36:12 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Tao Bao | 338be53 | 2017-07-11 16:45:04 -0700 | [diff] [blame] | 290 | const char* e2fsdroid_argv[] = { "/sbin/e2fsdroid_static", "-e", "-a", mount_point.c_str(), |
Jin Qian | ded2dac | 2017-04-21 14:36:12 -0700 | [diff] [blame] | 291 | location.c_str(), nullptr }; |
| 292 | status = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv)); |
| 293 | if (status != 0) { |
| 294 | LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 295 | return StringValue(""); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 296 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 297 | return StringValue(location); |
| 298 | } else if (fs_type == "f2fs") { |
| 299 | if (size < 0) { |
| 300 | LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size; |
| 301 | return StringValue(""); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 302 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 303 | std::string num_sectors = std::to_string(size / 512); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 304 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 305 | const char* f2fs_path = "/sbin/mkfs.f2fs"; |
Jaegeuk Kim | 1a8bb0f | 2017-11-01 13:24:43 -0700 | [diff] [blame] | 306 | const char* f2fs_argv[] = { "mkfs.f2fs", |
| 307 | "-d1", |
| 308 | "-f", |
| 309 | "-O", |
| 310 | "encrypt", |
| 311 | "-O", |
| 312 | "quota", |
| 313 | location.c_str(), |
| 314 | (size < 512) ? nullptr : num_sectors.c_str(), |
| 315 | nullptr }; |
Tao Bao | 397a813 | 2017-05-12 11:02:27 -0700 | [diff] [blame] | 316 | int status = exec_cmd(f2fs_path, const_cast<char**>(f2fs_argv)); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 317 | if (status != 0) { |
| 318 | LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location; |
| 319 | return StringValue(""); |
| 320 | } |
| 321 | return StringValue(location); |
| 322 | } else { |
| 323 | LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \"" |
| 324 | << partition_type << "\""; |
| 325 | } |
| 326 | |
| 327 | return nullptr; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 330 | Value* ShowProgressFn(const char* name, State* state, |
| 331 | const std::vector<std::unique_ptr<Expr>>& argv) { |
| 332 | if (argv.size() != 2) { |
| 333 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 334 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 335 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 336 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 337 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 338 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 339 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 340 | } |
| 341 | const std::string& frac_str = args[0]; |
| 342 | const std::string& sec_str = args[1]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 343 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 344 | double frac; |
| 345 | if (!android::base::ParseDouble(frac_str.c_str(), &frac)) { |
| 346 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name, |
| 347 | frac_str.c_str()); |
| 348 | } |
| 349 | int sec; |
| 350 | if (!android::base::ParseInt(sec_str.c_str(), &sec)) { |
| 351 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name, |
| 352 | sec_str.c_str()); |
| 353 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 354 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 355 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 356 | fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 357 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 358 | return StringValue(frac_str); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 361 | Value* SetProgressFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 362 | if (argv.size() != 1) { |
| 363 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 364 | } |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 365 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 366 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 367 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 368 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 369 | } |
| 370 | const std::string& frac_str = args[0]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 371 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 372 | double frac; |
| 373 | if (!android::base::ParseDouble(frac_str.c_str(), &frac)) { |
| 374 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name, |
| 375 | frac_str.c_str()); |
| 376 | } |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 377 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 378 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 379 | fprintf(ui->cmd_pipe, "set_progress %f\n", frac); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 380 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 381 | return StringValue(frac_str); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 384 | // package_extract_file(package_file[, dest_file]) |
| 385 | // Extracts a single package_file from the update package and writes it to dest_file, |
| 386 | // overwriting existing files if necessary. Without the dest_file argument, returns the |
| 387 | // contents of the package file as a binary blob. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 388 | Value* PackageExtractFileFn(const char* name, State* state, |
| 389 | const std::vector<std::unique_ptr<Expr>>& argv) { |
| 390 | if (argv.size() < 1 || argv.size() > 2) { |
| 391 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name, |
| 392 | argv.size()); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 393 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 394 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 395 | if (argv.size() == 2) { |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 396 | // The two-argument version extracts to a file. |
| 397 | |
| 398 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 399 | if (!ReadArgs(state, argv, &args)) { |
| 400 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name, |
| 401 | argv.size()); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 402 | } |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 403 | const std::string& zip_path = args[0]; |
| 404 | const std::string& dest_path = args[1]; |
Doug Zongker | 43772d2 | 2014-06-09 14:13:19 -0700 | [diff] [blame] | 405 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 406 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
| 407 | ZipString zip_string_path(zip_path.c_str()); |
| 408 | ZipEntry entry; |
| 409 | if (FindEntry(za, zip_string_path, &entry) != 0) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 410 | LOG(ERROR) << name << ": no " << zip_path << " in package"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 411 | return StringValue(""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 412 | } |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 413 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 414 | unique_fd fd(TEMP_FAILURE_RETRY( |
| 415 | ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR))); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 416 | if (fd == -1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 417 | PLOG(ERROR) << name << ": can't open " << dest_path << " for write"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 418 | return StringValue(""); |
| 419 | } |
| 420 | |
| 421 | bool success = true; |
| 422 | int32_t ret = ExtractEntryToFile(za, &entry, fd); |
| 423 | if (ret != 0) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 424 | LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" (" |
| 425 | << entry.uncompressed_length << " bytes) to \"" << dest_path |
| 426 | << "\": " << ErrorCodeString(ret); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 427 | success = false; |
| 428 | } |
| 429 | if (ota_fsync(fd) == -1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 430 | PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 431 | success = false; |
| 432 | } |
| 433 | if (ota_close(fd) == -1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 434 | PLOG(ERROR) << "close of \"" << dest_path << "\" failed"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 435 | success = false; |
| 436 | } |
| 437 | |
| 438 | return StringValue(success ? "t" : ""); |
| 439 | } else { |
| 440 | // The one-argument version returns the contents of the file as the result. |
| 441 | |
| 442 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 443 | if (!ReadArgs(state, argv, &args)) { |
| 444 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name, |
| 445 | argv.size()); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 446 | } |
| 447 | const std::string& zip_path = args[0]; |
| 448 | |
| 449 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
| 450 | ZipString zip_string_path(zip_path.c_str()); |
| 451 | ZipEntry entry; |
| 452 | if (FindEntry(za, zip_string_path, &entry) != 0) { |
| 453 | return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name, |
| 454 | zip_path.c_str()); |
| 455 | } |
| 456 | |
| 457 | std::string buffer; |
| 458 | buffer.resize(entry.uncompressed_length); |
| 459 | |
| 460 | int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size()); |
| 461 | if (ret != 0) { |
| 462 | return ErrorAbort(state, kPackageExtractFileFailure, |
| 463 | "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name, |
| 464 | zip_path.c_str(), buffer.size(), ErrorCodeString(ret)); |
| 465 | } |
| 466 | |
| 467 | return new Value(VAL_BLOB, buffer); |
| 468 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 471 | Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 472 | if (argv.size() != 1) { |
| 473 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 474 | } |
| 475 | std::string key; |
| 476 | if (!Evaluate(state, argv[0], &key)) { |
| 477 | return nullptr; |
| 478 | } |
| 479 | std::string value = android::base::GetProperty(key, ""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 480 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 481 | return StringValue(value); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 484 | // file_getprop(file, key) |
| 485 | // |
| 486 | // interprets 'file' as a getprop-style file (key=value pairs, one |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 487 | // per line. # comment lines, blank lines, lines without '=' ignored), |
Michael Runge | aa1a31e | 2014-04-25 18:47:18 -0700 | [diff] [blame] | 488 | // and returns the value for 'key' (or "" if it isn't defined). |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 489 | Value* FileGetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 490 | if (argv.size() != 2) { |
| 491 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 492 | argv.size()); |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 496 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 497 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 498 | } |
| 499 | const std::string& filename = args[0]; |
| 500 | const std::string& key = args[1]; |
| 501 | |
| 502 | struct stat st; |
| 503 | if (stat(filename.c_str(), &st) < 0) { |
| 504 | return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, |
| 505 | filename.c_str(), strerror(errno)); |
| 506 | } |
| 507 | |
| 508 | constexpr off_t MAX_FILE_GETPROP_SIZE = 65536; |
| 509 | if (st.st_size > MAX_FILE_GETPROP_SIZE) { |
| 510 | return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)", |
| 511 | filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE)); |
| 512 | } |
| 513 | |
| 514 | std::string buffer(st.st_size, '\0'); |
| 515 | unique_file f(ota_fopen(filename.c_str(), "rb")); |
| 516 | if (f == nullptr) { |
| 517 | return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename.c_str(), |
| 518 | strerror(errno)); |
| 519 | } |
| 520 | |
| 521 | if (ota_fread(&buffer[0], 1, st.st_size, f.get()) != static_cast<size_t>(st.st_size)) { |
| 522 | ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", name, |
| 523 | static_cast<size_t>(st.st_size), filename.c_str()); |
| 524 | return nullptr; |
| 525 | } |
| 526 | |
| 527 | ota_fclose(f); |
| 528 | |
| 529 | std::vector<std::string> lines = android::base::Split(buffer, "\n"); |
| 530 | for (size_t i = 0; i < lines.size(); i++) { |
| 531 | std::string line = android::base::Trim(lines[i]); |
| 532 | |
| 533 | // comment or blank line: skip to next line |
| 534 | if (line.empty() || line[0] == '#') { |
| 535 | continue; |
| 536 | } |
| 537 | size_t equal_pos = line.find('='); |
| 538 | if (equal_pos == std::string::npos) { |
| 539 | continue; |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 542 | // trim whitespace between key and '=' |
| 543 | std::string str = android::base::Trim(line.substr(0, equal_pos)); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 544 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 545 | // not the key we're looking for |
| 546 | if (key != str) continue; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 547 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 548 | return StringValue(android::base::Trim(line.substr(equal_pos + 1))); |
| 549 | } |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 550 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 551 | return StringValue(""); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 554 | // apply_patch_space(bytes) |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 555 | Value* ApplyPatchSpaceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 556 | if (argv.size() != 1) { |
| 557 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name, |
| 558 | argv.size()); |
| 559 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 560 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 561 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 562 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 563 | } |
| 564 | const std::string& bytes_str = args[0]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 565 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 566 | size_t bytes; |
| 567 | if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) { |
| 568 | return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n", |
| 569 | name, bytes_str.c_str()); |
| 570 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 571 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 572 | // Skip the cache size check if the update is a retry. |
| 573 | if (state->is_retry || CacheSizeCheck(bytes) == 0) { |
| 574 | return StringValue("t"); |
| 575 | } |
| 576 | return StringValue(""); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 577 | } |
| 578 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 579 | // apply_patch(src_file, tgt_file, tgt_sha1, tgt_size, patch1_sha1, patch1_blob, [...]) |
| 580 | // Applies a binary patch to the src_file to produce the tgt_file. If the desired target is the |
| 581 | // same as the source, pass "-" for tgt_file. tgt_sha1 and tgt_size are the expected final SHA1 |
| 582 | // hash and size of the target file. The remaining arguments must come in pairs: a SHA1 hash (a |
| 583 | // 40-character hex string) and a blob. The blob is the patch to be applied when the source |
| 584 | // file's current contents have the given SHA1. |
| 585 | // |
| 586 | // The patching is done in a safe manner that guarantees the target file either has the desired |
| 587 | // SHA1 hash and size, or it is untouched -- it will not be left in an unrecoverable intermediate |
| 588 | // state. If the process is interrupted during patching, the target file may be in an intermediate |
| 589 | // state; a copy exists in the cache partition so restarting the update can successfully update |
| 590 | // the file. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 591 | Value* ApplyPatchFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 592 | if (argv.size() < 6 || (argv.size() % 2) == 1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 593 | return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an " |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 594 | "even number, got %zu", name, argv.size()); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 597 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 598 | if (!ReadArgs(state, argv, &args, 0, 4)) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 599 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 600 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 601 | const std::string& source_filename = args[0]; |
| 602 | const std::string& target_filename = args[1]; |
| 603 | const std::string& target_sha1 = args[2]; |
| 604 | const std::string& target_size_str = args[3]; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 605 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 606 | size_t target_size; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 607 | if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) { |
| 608 | return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", |
| 609 | name, target_size_str.c_str()); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 610 | } |
| 611 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 612 | int patchcount = (argv.size()-4) / 2; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 613 | std::vector<std::unique_ptr<Value>> arg_values; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 614 | if (!ReadValueArgs(state, argv, &arg_values, 4, argv.size() - 4)) { |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 615 | return nullptr; |
| 616 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 617 | |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 618 | for (int i = 0; i < patchcount; ++i) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 619 | if (arg_values[i * 2]->type != VAL_STRING) { |
| 620 | return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, |
| 621 | i * 2); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 622 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 623 | if (arg_values[i * 2 + 1]->type != VAL_BLOB) { |
| 624 | return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, |
| 625 | i * 2 + 1); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 626 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 627 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 628 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 629 | std::vector<std::string> patch_sha_str; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 630 | std::vector<std::unique_ptr<Value>> patches; |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 631 | for (int i = 0; i < patchcount; ++i) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 632 | patch_sha_str.push_back(arg_values[i * 2]->data); |
| 633 | patches.push_back(std::move(arg_values[i * 2 + 1])); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 634 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 635 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 636 | int result = applypatch(source_filename.c_str(), target_filename.c_str(), |
| 637 | target_sha1.c_str(), target_size, |
| 638 | patch_sha_str, patches, nullptr); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 639 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 640 | return StringValue(result == 0 ? "t" : ""); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 643 | // apply_patch_check(filename, [sha1, ...]) |
| 644 | // Returns true if the contents of filename or the temporary copy in the cache partition (if |
| 645 | // present) have a SHA-1 checksum equal to one of the given sha1 values. sha1 values are |
| 646 | // specified as 40 hex digits. This function differs from sha1_check(read_file(filename), |
| 647 | // sha1 [, ...]) in that it knows to check the cache partition copy, so apply_patch_check() will |
| 648 | // succeed even if the file was corrupted by an interrupted apply_patch() update. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 649 | Value* ApplyPatchCheckFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 650 | if (argv.size() < 1) { |
| 651 | return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %zu", name, |
| 652 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 653 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 654 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 655 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 656 | if (!ReadArgs(state, argv, &args, 0, 1)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 657 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 658 | } |
| 659 | const std::string& filename = args[0]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 660 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 661 | std::vector<std::string> sha1s; |
Tao Bao | db56eb0 | 2017-03-23 06:34:20 -0700 | [diff] [blame] | 662 | if (argv.size() > 1 && !ReadArgs(state, argv, &sha1s, 1, argv.size() - 1)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 663 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 664 | } |
| 665 | int result = applypatch_check(filename.c_str(), sha1s); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 666 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 667 | return StringValue(result == 0 ? "t" : ""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Tao Bao | 0bbc764 | 2017-03-29 23:57:47 -0700 | [diff] [blame] | 670 | // This is the updater side handler for ui_print() in edify script. Contents will be sent over to |
| 671 | // the recovery side for on-screen display. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 672 | Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 673 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 674 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 0bbc764 | 2017-03-29 23:57:47 -0700 | [diff] [blame] | 675 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 676 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 677 | |
Tao Bao | 0bbc764 | 2017-03-29 23:57:47 -0700 | [diff] [blame] | 678 | std::string buffer = android::base::Join(args, ""); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 679 | uiPrint(state, buffer); |
| 680 | return StringValue(buffer); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 683 | Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 684 | if (!argv.empty()) { |
| 685 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name, |
| 686 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 687 | } |
| 688 | fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n"); |
| 689 | return StringValue("t"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 692 | Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 693 | if (argv.size() < 1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 694 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name); |
| 695 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 696 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 697 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 698 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 699 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 700 | } |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 701 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 702 | char* args2[argv.size() + 1]; |
| 703 | for (size_t i = 0; i < argv.size(); i++) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 704 | args2[i] = &args[i][0]; |
| 705 | } |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 706 | args2[argv.size()] = nullptr; |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 707 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 708 | LOG(INFO) << "about to run program [" << args2[0] << "] with " << argv.size() << " args"; |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 709 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 710 | pid_t child = fork(); |
| 711 | if (child == 0) { |
| 712 | execv(args2[0], args2); |
| 713 | PLOG(ERROR) << "run_program: execv failed"; |
Tao Bao | 3da8801 | 2017-02-03 13:09:23 -0800 | [diff] [blame] | 714 | _exit(EXIT_FAILURE); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 715 | } |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 716 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 717 | int status; |
| 718 | waitpid(child, &status, 0); |
| 719 | if (WIFEXITED(status)) { |
| 720 | if (WEXITSTATUS(status) != 0) { |
| 721 | LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status); |
| 722 | } |
| 723 | } else if (WIFSIGNALED(status)) { |
| 724 | LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status); |
| 725 | } |
| 726 | |
| 727 | return StringValue(std::to_string(status)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 730 | // sha1_check(data) |
| 731 | // to return the sha1 of the data (given in the format returned by |
| 732 | // read_file). |
| 733 | // |
| 734 | // sha1_check(data, sha1_hex, [sha1_hex, ...]) |
| 735 | // returns the sha1 of the file if it matches any of the hex |
| 736 | // strings passed, or "" if it does not equal any of them. |
| 737 | // |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 738 | Value* Sha1CheckFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 739 | if (argv.size() < 1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 740 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name); |
| 741 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 742 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 743 | std::vector<std::unique_ptr<Value>> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 744 | if (!ReadValueArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 745 | return nullptr; |
| 746 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 747 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 748 | if (args[0]->type == VAL_INVALID) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 749 | return StringValue(""); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 750 | } |
| 751 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 752 | SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest); |
| 753 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 754 | if (argv.size() == 1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 755 | return StringValue(print_sha1(digest)); |
| 756 | } |
| 757 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 758 | for (size_t i = 1; i < argv.size(); ++i) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 759 | uint8_t arg_digest[SHA_DIGEST_LENGTH]; |
| 760 | if (args[i]->type != VAL_STRING) { |
| 761 | LOG(ERROR) << name << "(): arg " << i << " is not a string; skipping"; |
| 762 | } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) { |
| 763 | // Warn about bad args and skip them. |
| 764 | LOG(ERROR) << name << "(): error parsing \"" << args[i]->data << "\" as sha-1; skipping"; |
| 765 | } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) { |
| 766 | // Found a match. |
| 767 | return args[i].release(); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | // Didn't match any of the hex strings; return false. |
| 772 | return StringValue(""); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 773 | } |
| 774 | |
Hristo Bojinov | db314d6 | 2010-08-02 10:29:49 -0700 | [diff] [blame] | 775 | // Read a local file and return its contents (the Value* returned |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 776 | // is actually a FileContents*). |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 777 | Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 778 | if (argv.size() != 1) { |
| 779 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 780 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 781 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 782 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 783 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 784 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 785 | } |
| 786 | const std::string& filename = args[0]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 787 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 788 | Value* v = new Value(VAL_INVALID, ""); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 789 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 790 | FileContents fc; |
| 791 | if (LoadFileContents(filename.c_str(), &fc) == 0) { |
| 792 | v->type = VAL_BLOB; |
| 793 | v->data = std::string(fc.data.begin(), fc.data.end()); |
| 794 | } |
| 795 | return v; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 796 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 797 | |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 798 | // write_value(value, filename) |
| 799 | // Writes 'value' to 'filename'. |
| 800 | // Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq") |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 801 | Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 802 | if (argv.size() != 2) { |
| 803 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 804 | argv.size()); |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 808 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 809 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
| 810 | } |
| 811 | |
| 812 | const std::string& filename = args[1]; |
| 813 | if (filename.empty()) { |
| 814 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name); |
| 815 | } |
| 816 | |
| 817 | const std::string& value = args[0]; |
| 818 | if (!android::base::WriteStringToFile(value, filename)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 819 | PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\""; |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 820 | return StringValue(""); |
| 821 | } else { |
| 822 | return StringValue("t"); |
| 823 | } |
| 824 | } |
| 825 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 826 | // Immediately reboot the device. Recovery is not finished normally, |
| 827 | // so if you reboot into recovery it will re-start applying the |
| 828 | // current package (because nothing has cleared the copy of the |
| 829 | // arguments stored in the BCB). |
| 830 | // |
| 831 | // The argument is the partition name passed to the android reboot |
| 832 | // property. It can be "recovery" to boot from the recovery |
| 833 | // partition, or "" (empty string) to boot from the regular boot |
| 834 | // partition. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 835 | Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 836 | if (argv.size() != 2) { |
| 837 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 838 | argv.size()); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 839 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 840 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 841 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 842 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 843 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
| 844 | } |
| 845 | const std::string& filename = args[0]; |
| 846 | const std::string& property = args[1]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 847 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 848 | // Zero out the 'command' field of the bootloader message. Leave the rest intact. |
| 849 | bootloader_message boot; |
| 850 | std::string err; |
| 851 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 852 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 853 | return StringValue(""); |
| 854 | } |
| 855 | memset(boot.command, 0, sizeof(boot.command)); |
| 856 | if (!write_bootloader_message_to(boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 857 | LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 858 | return StringValue(""); |
| 859 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 860 | |
Dmitri Plotnikov | 8706a98 | 2017-04-18 08:28:26 -0700 | [diff] [blame] | 861 | std::string reboot_cmd = "reboot," + property; |
| 862 | if (android::base::GetBoolProperty("ro.boot.quiescent", false)) { |
| 863 | reboot_cmd += ",quiescent"; |
| 864 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 865 | android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 866 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 867 | sleep(5); |
| 868 | return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | // Store a string value somewhere that future invocations of recovery |
| 872 | // can access it. This value is called the "stage" and can be used to |
| 873 | // drive packages that need to do reboots in the middle of |
| 874 | // installation and keep track of where they are in the multi-stage |
| 875 | // install. |
| 876 | // |
| 877 | // The first argument is the block device for the misc partition |
| 878 | // ("/misc" in the fstab), which is where this value is stored. The |
| 879 | // second argument is the string to store; it should not exceed 31 |
| 880 | // bytes. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 881 | Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 882 | if (argv.size() != 2) { |
| 883 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 884 | argv.size()); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 885 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 886 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 887 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 888 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 889 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 890 | } |
| 891 | const std::string& filename = args[0]; |
| 892 | const std::string& stagestr = args[1]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 893 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 894 | // Store this value in the misc partition, immediately after the |
| 895 | // bootloader message that the main recovery uses to save its |
| 896 | // arguments in case of the device restarting midway through |
| 897 | // package installation. |
| 898 | bootloader_message boot; |
| 899 | std::string err; |
| 900 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 901 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 902 | return StringValue(""); |
| 903 | } |
| 904 | strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage)); |
| 905 | if (!write_bootloader_message_to(boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 906 | LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 907 | return StringValue(""); |
| 908 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 909 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 910 | return StringValue(filename); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | // Return the value most recently saved with SetStageFn. The argument |
| 914 | // is the block device for the misc partition. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 915 | Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 916 | if (argv.size() != 1) { |
| 917 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size()); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 918 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 919 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 920 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 921 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 922 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 923 | } |
| 924 | const std::string& filename = args[0]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 925 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 926 | bootloader_message boot; |
| 927 | std::string err; |
| 928 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 929 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 930 | return StringValue(""); |
| 931 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 932 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 933 | return StringValue(boot.stage); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 934 | } |
| 935 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 936 | Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 937 | if (argv.size() != 2) { |
| 938 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 939 | argv.size()); |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 940 | } |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 941 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 942 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 943 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 944 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 945 | } |
| 946 | const std::string& filename = args[0]; |
| 947 | const std::string& len_str = args[1]; |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 948 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 949 | size_t len; |
| 950 | if (!android::base::ParseUint(len_str.c_str(), &len)) { |
| 951 | return nullptr; |
| 952 | } |
| 953 | unique_fd fd(ota_open(filename.c_str(), O_WRONLY, 0644)); |
| 954 | // The wipe_block_device function in ext4_utils returns 0 on success and 1 |
| 955 | // for failure. |
| 956 | int status = wipe_block_device(fd, len); |
| 957 | return StringValue((status == 0) ? "t" : ""); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 958 | } |
| 959 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 960 | Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 961 | if (!argv.empty()) { |
| 962 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name, |
| 963 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 964 | } |
| 965 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 966 | fprintf(ui->cmd_pipe, "enable_reboot\n"); |
| 967 | return StringValue("t"); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 970 | Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 971 | if (argv.empty()) { |
| 972 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 973 | } |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 974 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 975 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 976 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 977 | return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name); |
| 978 | } |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 979 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 980 | char* args2[argv.size() + 1]; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 981 | // Tune2fs expects the program name as its args[0] |
| 982 | args2[0] = const_cast<char*>(name); |
| 983 | if (args2[0] == nullptr) { |
| 984 | return nullptr; |
| 985 | } |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 986 | for (size_t i = 0; i < argv.size(); ++i) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 987 | args2[i + 1] = &args[i][0]; |
| 988 | } |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 989 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 990 | // tune2fs changes the file system parameters on an ext2 file system; it |
| 991 | // returns 0 on success. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 992 | int result = tune2fs_main(argv.size() + 1, args2); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 993 | if (result != 0) { |
| 994 | return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result); |
| 995 | } |
| 996 | return StringValue("t"); |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 997 | } |
| 998 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 999 | void RegisterInstallFunctions() { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1000 | RegisterFunction("mount", MountFn); |
| 1001 | RegisterFunction("is_mounted", IsMountedFn); |
| 1002 | RegisterFunction("unmount", UnmountFn); |
| 1003 | RegisterFunction("format", FormatFn); |
| 1004 | RegisterFunction("show_progress", ShowProgressFn); |
| 1005 | RegisterFunction("set_progress", SetProgressFn); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1006 | RegisterFunction("package_extract_file", PackageExtractFileFn); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1007 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1008 | RegisterFunction("getprop", GetPropFn); |
| 1009 | RegisterFunction("file_getprop", FileGetPropFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1010 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1011 | RegisterFunction("apply_patch", ApplyPatchFn); |
| 1012 | RegisterFunction("apply_patch_check", ApplyPatchCheckFn); |
| 1013 | RegisterFunction("apply_patch_space", ApplyPatchSpaceFn); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1014 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1015 | RegisterFunction("wipe_block_device", WipeBlockDeviceFn); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1016 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1017 | RegisterFunction("read_file", ReadFileFn); |
| 1018 | RegisterFunction("sha1_check", Sha1CheckFn); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1019 | RegisterFunction("write_value", WriteValueFn); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1020 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1021 | RegisterFunction("wipe_cache", WipeCacheFn); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1022 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1023 | RegisterFunction("ui_print", UIPrintFn); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1024 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1025 | RegisterFunction("run_program", RunProgramFn); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1026 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1027 | RegisterFunction("reboot_now", RebootNowFn); |
| 1028 | RegisterFunction("get_stage", GetStageFn); |
| 1029 | RegisterFunction("set_stage", SetStageFn); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 1030 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1031 | RegisterFunction("enable_reboot", EnableRebootFn); |
| 1032 | RegisterFunction("tune2fs", Tune2FsFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1033 | } |