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/make_ext4fs.h> |
| 53 | #include <ext4_utils/wipe.h> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 54 | #include <openssl/sha.h> |
Elliott Hughes | 4bbd5bf | 2016-04-01 18:24:39 -0700 | [diff] [blame] | 55 | #include <selinux/label.h> |
| 56 | #include <selinux/selinux.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" |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 60 | #include "error_code.h" |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 61 | #include "mounts.h" |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 62 | #include "ota_io.h" |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 63 | #include "otautil/DirUtil.h" |
| 64 | #include "otautil/ZipUtil.h" |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 65 | #include "print_sha1.h" |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 66 | #include "tune2fs.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 67 | #include "updater/updater.h" |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 68 | |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 69 | // Send over the buffer to recovery though the command pipe. |
| 70 | static void uiPrint(State* state, const std::string& buffer) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 71 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 72 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 73 | // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "". |
| 74 | // So skip sending empty strings to UI. |
| 75 | std::vector<std::string> lines = android::base::Split(buffer, "\n"); |
| 76 | for (auto& line : lines) { |
| 77 | if (!line.empty()) { |
| 78 | fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str()); |
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 | } |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 81 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 82 | // On the updater side, we need to dump the contents to stderr (which has |
| 83 | // been redirected to the log file). Because the recovery will only print |
| 84 | // the contents to screen when processing pipe command ui_print. |
| 85 | LOG(INFO) << buffer; |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Elliott Hughes | 83ce755 | 2016-06-30 09:28:42 -0700 | [diff] [blame] | 88 | void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 89 | std::string error_msg; |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 90 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 91 | va_list ap; |
| 92 | va_start(ap, format); |
| 93 | android::base::StringAppendV(&error_msg, format, ap); |
| 94 | va_end(ap); |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 95 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 96 | uiPrint(state, error_msg); |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Tianjie Xu | d75003d | 2016-11-04 11:31:29 -0700 | [diff] [blame] | 99 | static bool is_dir(const std::string& dirpath) { |
| 100 | struct stat st; |
| 101 | return stat(dirpath.c_str(), &st) == 0 && S_ISDIR(st.st_mode); |
| 102 | } |
| 103 | |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 104 | // Create all parent directories of name, if necessary. |
Tianjie Xu | d75003d | 2016-11-04 11:31:29 -0700 | [diff] [blame] | 105 | static bool make_parents(const std::string& name) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 106 | size_t prev_end = 0; |
| 107 | while (prev_end < name.size()) { |
| 108 | size_t next_end = name.find('/', prev_end + 1); |
| 109 | if (next_end == std::string::npos) { |
| 110 | break; |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 111 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 112 | std::string dir_path = name.substr(0, next_end); |
| 113 | if (!is_dir(dir_path)) { |
| 114 | int result = mkdir(dir_path.c_str(), 0700); |
| 115 | if (result != 0) { |
| 116 | PLOG(ERROR) << "failed to mkdir " << dir_path << " when make parents for " << name; |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | LOG(INFO) << "created [" << dir_path << "]"; |
| 121 | } |
| 122 | prev_end = next_end; |
| 123 | } |
| 124 | return true; |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 127 | // mount(fs_type, partition_type, location, mount_point) |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 128 | // mount(fs_type, partition_type, location, mount_point, mount_options) |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 129 | // |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 130 | // fs_type="ext4" partition_type="EMMC" location=device |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 131 | Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 132 | if (argc != 4 && argc != 5) { |
| 133 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc); |
| 134 | } |
| 135 | |
| 136 | std::vector<std::string> args; |
| 137 | if (!ReadArgs(state, argc, argv, &args)) { |
| 138 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 139 | } |
| 140 | const std::string& fs_type = args[0]; |
| 141 | const std::string& partition_type = args[1]; |
| 142 | const std::string& location = args[2]; |
| 143 | const std::string& mount_point = args[3]; |
| 144 | std::string mount_options; |
| 145 | |
| 146 | if (argc == 5) { |
| 147 | mount_options = args[4]; |
| 148 | } |
| 149 | |
| 150 | if (fs_type.empty()) { |
| 151 | return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name); |
| 152 | } |
| 153 | if (partition_type.empty()) { |
| 154 | return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty", |
| 155 | name); |
| 156 | } |
| 157 | if (location.empty()) { |
| 158 | return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name); |
| 159 | } |
| 160 | if (mount_point.empty()) { |
| 161 | return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty", |
| 162 | name); |
| 163 | } |
| 164 | |
| 165 | { |
| 166 | char* secontext = nullptr; |
| 167 | |
| 168 | if (sehandle) { |
| 169 | selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755); |
| 170 | setfscreatecon(secontext); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 171 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 172 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 173 | mkdir(mount_point.c_str(), 0755); |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 174 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 175 | if (secontext) { |
| 176 | freecon(secontext); |
| 177 | setfscreatecon(nullptr); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 178 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 179 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 180 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 181 | if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(), |
| 182 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) { |
| 183 | uiPrintf(state, "%s: failed to mount %s at %s: %s\n", name, location.c_str(), |
| 184 | mount_point.c_str(), strerror(errno)); |
| 185 | return StringValue(""); |
| 186 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 187 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 188 | return StringValue(mount_point); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 191 | |
| 192 | // is_mounted(mount_point) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 193 | Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 194 | if (argc != 1) { |
| 195 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
| 196 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 197 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 198 | std::vector<std::string> args; |
| 199 | if (!ReadArgs(state, argc, argv, &args)) { |
| 200 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 201 | } |
| 202 | const std::string& mount_point = args[0]; |
| 203 | if (mount_point.empty()) { |
| 204 | return ErrorAbort(state, kArgsParsingFailure, |
| 205 | "mount_point argument to unmount() can't be empty"); |
| 206 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 207 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 208 | scan_mounted_volumes(); |
| 209 | MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str()); |
| 210 | if (vol == nullptr) { |
| 211 | return StringValue(""); |
| 212 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 213 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 214 | return StringValue(mount_point); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 217 | Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 218 | if (argc != 1) { |
| 219 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
| 220 | } |
| 221 | std::vector<std::string> args; |
| 222 | if (!ReadArgs(state, argc, argv, &args)) { |
| 223 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 224 | } |
| 225 | const std::string& mount_point = args[0]; |
| 226 | if (mount_point.empty()) { |
| 227 | return ErrorAbort(state, kArgsParsingFailure, |
| 228 | "mount_point argument to unmount() can't be empty"); |
| 229 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 230 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 231 | scan_mounted_volumes(); |
| 232 | MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str()); |
| 233 | if (vol == nullptr) { |
| 234 | uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point.c_str()); |
| 235 | return nullptr; |
| 236 | } else { |
| 237 | int ret = unmount_mounted_volume(vol); |
| 238 | if (ret != 0) { |
| 239 | uiPrintf(state, "unmount of %s failed (%d): %s\n", mount_point.c_str(), ret, strerror(errno)); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 240 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 241 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 242 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 243 | return StringValue(mount_point); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 244 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 245 | |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 246 | static int exec_cmd(const char* path, char* const argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 247 | pid_t child; |
| 248 | if ((child = vfork()) == 0) { |
| 249 | execv(path, argv); |
| 250 | _exit(-1); |
| 251 | } |
| 252 | |
| 253 | int status; |
| 254 | waitpid(child, &status, 0); |
| 255 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 256 | LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status); |
| 257 | } |
| 258 | return WEXITSTATUS(status); |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 261 | // format(fs_type, partition_type, location, fs_size, mount_point) |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 262 | // |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 263 | // fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location> |
| 264 | // 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] | 265 | // if fs_size == 0, then make fs uses the entire partition. |
Ken Sumrall | 8f132ed | 2011-01-14 18:55:05 -0800 | [diff] [blame] | 266 | // if fs_size > 0, that is the size to use |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 267 | // if fs_size < 0, then reserve that many bytes at the end of the partition (not for "f2fs") |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 268 | Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 269 | if (argc != 5) { |
| 270 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc); |
| 271 | } |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 272 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 273 | std::vector<std::string> args; |
| 274 | if (!ReadArgs(state, argc, argv, &args)) { |
| 275 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 276 | } |
| 277 | const std::string& fs_type = args[0]; |
| 278 | const std::string& partition_type = args[1]; |
| 279 | const std::string& location = args[2]; |
| 280 | const std::string& fs_size = args[3]; |
| 281 | const std::string& mount_point = args[4]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 282 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 283 | if (fs_type.empty()) { |
| 284 | return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name); |
| 285 | } |
| 286 | if (partition_type.empty()) { |
| 287 | return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty", |
| 288 | name); |
| 289 | } |
| 290 | if (location.empty()) { |
| 291 | return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name); |
| 292 | } |
| 293 | if (mount_point.empty()) { |
| 294 | return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty", |
| 295 | name); |
| 296 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 297 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 298 | int64_t size; |
| 299 | if (!android::base::ParseInt(fs_size, &size)) { |
| 300 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name, |
| 301 | fs_size.c_str()); |
| 302 | } |
| 303 | |
| 304 | if (fs_type == "ext4") { |
| 305 | int status = make_ext4fs(location.c_str(), size, mount_point.c_str(), sehandle); |
| 306 | if (status != 0) { |
| 307 | LOG(ERROR) << name << ": make_ext4fs failed (" << status << ") on " << location; |
| 308 | return StringValue(""); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 309 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 310 | return StringValue(location); |
| 311 | } else if (fs_type == "f2fs") { |
| 312 | if (size < 0) { |
| 313 | LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size; |
| 314 | return StringValue(""); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 315 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 316 | std::string num_sectors = std::to_string(size / 512); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 317 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 318 | const char* f2fs_path = "/sbin/mkfs.f2fs"; |
| 319 | const char* const f2fs_argv[] = { "mkfs.f2fs", "-t", "-d1", location.c_str(), |
| 320 | num_sectors.c_str(), nullptr }; |
| 321 | int status = exec_cmd(f2fs_path, const_cast<char* const*>(f2fs_argv)); |
| 322 | if (status != 0) { |
| 323 | LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location; |
| 324 | return StringValue(""); |
| 325 | } |
| 326 | return StringValue(location); |
| 327 | } else { |
| 328 | LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \"" |
| 329 | << partition_type << "\""; |
| 330 | } |
| 331 | |
| 332 | return nullptr; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Tao Bao | a659d79 | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 335 | // rename(src_name, dst_name) |
| 336 | // Renames src_name to dst_name. It automatically creates the necessary directories for dst_name. |
| 337 | // Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk") |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 338 | Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 339 | if (argc != 2) { |
| 340 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 341 | } |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 342 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 343 | std::vector<std::string> args; |
| 344 | if (!ReadArgs(state, argc, argv, &args)) { |
| 345 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 346 | } |
| 347 | const std::string& src_name = args[0]; |
| 348 | const std::string& dst_name = args[1]; |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 349 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 350 | if (src_name.empty()) { |
| 351 | return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name); |
| 352 | } |
| 353 | if (dst_name.empty()) { |
| 354 | return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name); |
| 355 | } |
| 356 | if (!make_parents(dst_name)) { |
| 357 | return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s", |
| 358 | dst_name.c_str(), strerror(errno)); |
| 359 | } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) { |
| 360 | // File was already moved |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 361 | return StringValue(dst_name); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 362 | } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) { |
| 363 | return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s", |
| 364 | src_name.c_str(), dst_name.c_str(), strerror(errno)); |
| 365 | } |
| 366 | |
| 367 | return StringValue(dst_name); |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 368 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 369 | |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 370 | // delete([filename, ...]) |
| 371 | // Deletes all the filenames listed. Returns the number of files successfully deleted. |
| 372 | // |
| 373 | // delete_recursive([dirname, ...]) |
| 374 | // Recursively deletes dirnames and all their contents. Returns the number of directories |
| 375 | // successfully deleted. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 376 | Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 377 | std::vector<std::string> paths(argc); |
| 378 | for (int i = 0; i < argc; ++i) { |
| 379 | if (!Evaluate(state, argv[i], &paths[i])) { |
| 380 | return nullptr; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 381 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 382 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 383 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 384 | bool recursive = (strcmp(name, "delete_recursive") == 0); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 385 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 386 | int success = 0; |
| 387 | for (int i = 0; i < argc; ++i) { |
| 388 | if ((recursive ? dirUnlinkHierarchy(paths[i].c_str()) : unlink(paths[i].c_str())) == 0) { |
| 389 | ++success; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 390 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 391 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 392 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 393 | return StringValue(std::to_string(success)); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 396 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 397 | Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 398 | if (argc != 2) { |
| 399 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 400 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 401 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 402 | std::vector<std::string> args; |
| 403 | if (!ReadArgs(state, argc, argv, &args)) { |
| 404 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 405 | } |
| 406 | const std::string& frac_str = args[0]; |
| 407 | const std::string& sec_str = args[1]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 408 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 409 | double frac; |
| 410 | if (!android::base::ParseDouble(frac_str.c_str(), &frac)) { |
| 411 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name, |
| 412 | frac_str.c_str()); |
| 413 | } |
| 414 | int sec; |
| 415 | if (!android::base::ParseInt(sec_str.c_str(), &sec)) { |
| 416 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name, |
| 417 | sec_str.c_str()); |
| 418 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 419 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 420 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 421 | fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 422 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 423 | return StringValue(frac_str); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 426 | Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 427 | if (argc != 1) { |
| 428 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
| 429 | } |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 430 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 431 | std::vector<std::string> args; |
| 432 | if (!ReadArgs(state, 1, argv, &args)) { |
| 433 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 434 | } |
| 435 | const std::string& frac_str = args[0]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 436 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 437 | double frac; |
| 438 | if (!android::base::ParseDouble(frac_str.c_str(), &frac)) { |
| 439 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name, |
| 440 | frac_str.c_str()); |
| 441 | } |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 442 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 443 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 444 | fprintf(ui->cmd_pipe, "set_progress %f\n", frac); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 445 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 446 | return StringValue(frac_str); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 449 | // package_extract_dir(package_dir, dest_dir) |
| 450 | // Extracts all files from the package underneath package_dir and writes them to the |
| 451 | // corresponding tree beneath dest_dir. Any existing files are overwritten. |
| 452 | // Example: package_extract_dir("system", "/system") |
| 453 | // |
| 454 | // Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path. |
| 455 | Value* PackageExtractDirFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 456 | if (argc != 2) { |
| 457 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 458 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 459 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 460 | std::vector<std::string> args; |
| 461 | if (!ReadArgs(state, 2, argv, &args)) { |
| 462 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 463 | } |
| 464 | const std::string& zip_path = args[0]; |
| 465 | const std::string& dest_path = args[1]; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 466 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 467 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 468 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 469 | // To create a consistent system image, never use the clock for timestamps. |
| 470 | constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 471 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 472 | bool success = ExtractPackageRecursive(za, zip_path, dest_path, ×tamp, sehandle); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 473 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 474 | return StringValue(success ? "t" : ""); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 477 | // package_extract_file(package_file[, dest_file]) |
| 478 | // Extracts a single package_file from the update package and writes it to dest_file, |
| 479 | // overwriting existing files if necessary. Without the dest_file argument, returns the |
| 480 | // contents of the package file as a binary blob. |
| 481 | Value* PackageExtractFileFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 482 | if (argc < 1 || argc > 2) { |
| 483 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d", name, argc); |
| 484 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 485 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 486 | if (argc == 2) { |
| 487 | // The two-argument version extracts to a file. |
| 488 | |
| 489 | std::vector<std::string> args; |
| 490 | if (!ReadArgs(state, 2, argv, &args)) { |
| 491 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 492 | } |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 493 | const std::string& zip_path = args[0]; |
| 494 | const std::string& dest_path = args[1]; |
Doug Zongker | 43772d2 | 2014-06-09 14:13:19 -0700 | [diff] [blame] | 495 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 496 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
| 497 | ZipString zip_string_path(zip_path.c_str()); |
| 498 | ZipEntry entry; |
| 499 | if (FindEntry(za, zip_string_path, &entry) != 0) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 500 | LOG(ERROR) << name << ": no " << zip_path << " in package"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 501 | return StringValue(""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 502 | } |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 503 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 504 | unique_fd fd(TEMP_FAILURE_RETRY( |
| 505 | 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] | 506 | if (fd == -1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 507 | PLOG(ERROR) << name << ": can't open " << dest_path << " for write"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 508 | return StringValue(""); |
| 509 | } |
| 510 | |
| 511 | bool success = true; |
| 512 | int32_t ret = ExtractEntryToFile(za, &entry, fd); |
| 513 | if (ret != 0) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 514 | LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" (" |
| 515 | << entry.uncompressed_length << " bytes) to \"" << dest_path |
| 516 | << "\": " << ErrorCodeString(ret); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 517 | success = false; |
| 518 | } |
| 519 | if (ota_fsync(fd) == -1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 520 | PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 521 | success = false; |
| 522 | } |
| 523 | if (ota_close(fd) == -1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 524 | PLOG(ERROR) << "close of \"" << dest_path << "\" failed"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 525 | success = false; |
| 526 | } |
| 527 | |
| 528 | return StringValue(success ? "t" : ""); |
| 529 | } else { |
| 530 | // The one-argument version returns the contents of the file as the result. |
| 531 | |
| 532 | std::vector<std::string> args; |
| 533 | if (!ReadArgs(state, 1, argv, &args)) { |
| 534 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name, argc); |
| 535 | } |
| 536 | const std::string& zip_path = args[0]; |
| 537 | |
| 538 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
| 539 | ZipString zip_string_path(zip_path.c_str()); |
| 540 | ZipEntry entry; |
| 541 | if (FindEntry(za, zip_string_path, &entry) != 0) { |
| 542 | return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name, |
| 543 | zip_path.c_str()); |
| 544 | } |
| 545 | |
| 546 | std::string buffer; |
| 547 | buffer.resize(entry.uncompressed_length); |
| 548 | |
| 549 | int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size()); |
| 550 | if (ret != 0) { |
| 551 | return ErrorAbort(state, kPackageExtractFileFailure, |
| 552 | "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name, |
| 553 | zip_path.c_str(), buffer.size(), ErrorCodeString(ret)); |
| 554 | } |
| 555 | |
| 556 | return new Value(VAL_BLOB, buffer); |
| 557 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Tao Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 560 | // symlink(target, [src1, src2, ...]) |
| 561 | // Creates all sources as symlinks to target. It unlinks any previously existing src1, src2, etc |
| 562 | // before creating symlinks. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 563 | Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 564 | if (argc == 0) { |
| 565 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc); |
| 566 | } |
| 567 | std::string target; |
| 568 | if (!Evaluate(state, argv[0], &target)) { |
| 569 | return nullptr; |
| 570 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 571 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 572 | std::vector<std::string> srcs; |
| 573 | if (!ReadArgs(state, argc - 1, argv + 1, &srcs)) { |
| 574 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
| 575 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 576 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 577 | size_t bad = 0; |
| 578 | for (const auto& src : srcs) { |
| 579 | if (unlink(src.c_str()) == -1 && errno != ENOENT) { |
| 580 | PLOG(ERROR) << name << ": failed to remove " << src; |
| 581 | ++bad; |
| 582 | } else if (!make_parents(src)) { |
| 583 | LOG(ERROR) << name << ": failed to symlink " << src << " to " << target |
| 584 | << ": making parents failed"; |
| 585 | ++bad; |
| 586 | } else if (symlink(target.c_str(), src.c_str()) == -1) { |
| 587 | PLOG(ERROR) << name << ": failed to symlink " << src << " to " << target; |
| 588 | ++bad; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 589 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 590 | } |
| 591 | if (bad != 0) { |
| 592 | return ErrorAbort(state, kSymlinkFailure, "%s: Failed to create %zu symlink(s)", name, bad); |
| 593 | } |
| 594 | return StringValue("t"); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 597 | struct perm_parsed_args { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 598 | bool has_uid; |
| 599 | uid_t uid; |
| 600 | bool has_gid; |
| 601 | gid_t gid; |
| 602 | bool has_mode; |
| 603 | mode_t mode; |
| 604 | bool has_fmode; |
| 605 | mode_t fmode; |
| 606 | bool has_dmode; |
| 607 | mode_t dmode; |
| 608 | bool has_selabel; |
| 609 | const char* selabel; |
| 610 | bool has_capabilities; |
| 611 | uint64_t capabilities; |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 612 | }; |
| 613 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 614 | static struct perm_parsed_args ParsePermArgs(State * state, int argc, |
| 615 | const std::vector<std::string>& args) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 616 | struct perm_parsed_args parsed; |
| 617 | int bad = 0; |
| 618 | static int max_warnings = 20; |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 619 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 620 | memset(&parsed, 0, sizeof(parsed)); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 621 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 622 | for (int i = 1; i < argc; i += 2) { |
| 623 | if (args[i] == "uid") { |
| 624 | int64_t uid; |
| 625 | if (sscanf(args[i + 1].c_str(), "%" SCNd64, &uid) == 1) { |
| 626 | parsed.uid = uid; |
| 627 | parsed.has_uid = true; |
| 628 | } else { |
| 629 | uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str()); |
| 630 | bad++; |
| 631 | } |
| 632 | continue; |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 633 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 634 | if (args[i] == "gid") { |
| 635 | int64_t gid; |
| 636 | if (sscanf(args[i + 1].c_str(), "%" SCNd64, &gid) == 1) { |
| 637 | parsed.gid = gid; |
| 638 | parsed.has_gid = true; |
| 639 | } else { |
| 640 | uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str()); |
| 641 | bad++; |
| 642 | } |
| 643 | continue; |
| 644 | } |
| 645 | if (args[i] == "mode") { |
| 646 | int32_t mode; |
| 647 | if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) { |
| 648 | parsed.mode = mode; |
| 649 | parsed.has_mode = true; |
| 650 | } else { |
| 651 | uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str()); |
| 652 | bad++; |
| 653 | } |
| 654 | continue; |
| 655 | } |
| 656 | if (args[i] == "dmode") { |
| 657 | int32_t mode; |
| 658 | if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) { |
| 659 | parsed.dmode = mode; |
| 660 | parsed.has_dmode = true; |
| 661 | } else { |
| 662 | uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str()); |
| 663 | bad++; |
| 664 | } |
| 665 | continue; |
| 666 | } |
| 667 | if (args[i] == "fmode") { |
| 668 | int32_t mode; |
| 669 | if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) { |
| 670 | parsed.fmode = mode; |
| 671 | parsed.has_fmode = true; |
| 672 | } else { |
| 673 | uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str()); |
| 674 | bad++; |
| 675 | } |
| 676 | continue; |
| 677 | } |
| 678 | if (args[i] == "capabilities") { |
| 679 | int64_t capabilities; |
| 680 | if (sscanf(args[i + 1].c_str(), "%" SCNi64, &capabilities) == 1) { |
| 681 | parsed.capabilities = capabilities; |
| 682 | parsed.has_capabilities = true; |
| 683 | } else { |
| 684 | uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str()); |
| 685 | bad++; |
| 686 | } |
| 687 | continue; |
| 688 | } |
| 689 | if (args[i] == "selabel") { |
| 690 | if (!args[i + 1].empty()) { |
| 691 | parsed.selabel = args[i + 1].c_str(); |
| 692 | parsed.has_selabel = true; |
| 693 | } else { |
| 694 | uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str()); |
| 695 | bad++; |
| 696 | } |
| 697 | continue; |
| 698 | } |
| 699 | if (max_warnings != 0) { |
| 700 | printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str()); |
| 701 | max_warnings--; |
| 702 | if (max_warnings == 0) { |
| 703 | LOG(INFO) << "ParsedPermArgs: suppressing further warnings"; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | return parsed; |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 710 | static int ApplyParsedPerms(State* state, const char* filename, const struct stat* statptr, |
| 711 | struct perm_parsed_args parsed) { |
| 712 | int bad = 0; |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 713 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 714 | if (parsed.has_selabel) { |
| 715 | if (lsetfilecon(filename, parsed.selabel) != 0) { |
| 716 | uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n", filename, |
| 717 | parsed.selabel, strerror(errno)); |
| 718 | bad++; |
Nick Kralevich | 6880241 | 2014-10-23 20:36:42 -0700 | [diff] [blame] | 719 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 720 | } |
Nick Kralevich | 6880241 | 2014-10-23 20:36:42 -0700 | [diff] [blame] | 721 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 722 | /* ignore symlinks */ |
| 723 | if (S_ISLNK(statptr->st_mode)) { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 724 | return bad; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | if (parsed.has_uid) { |
| 728 | if (chown(filename, parsed.uid, -1) < 0) { |
| 729 | uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n", filename, parsed.uid, |
| 730 | strerror(errno)); |
| 731 | bad++; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | if (parsed.has_gid) { |
| 736 | if (chown(filename, -1, parsed.gid) < 0) { |
| 737 | uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n", filename, parsed.gid, |
| 738 | strerror(errno)); |
| 739 | bad++; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | if (parsed.has_mode) { |
| 744 | if (chmod(filename, parsed.mode) < 0) { |
| 745 | uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.mode, |
| 746 | strerror(errno)); |
| 747 | bad++; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) { |
| 752 | if (chmod(filename, parsed.dmode) < 0) { |
| 753 | uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.dmode, |
| 754 | strerror(errno)); |
| 755 | bad++; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | if (parsed.has_fmode && S_ISREG(statptr->st_mode)) { |
| 760 | if (chmod(filename, parsed.fmode) < 0) { |
| 761 | uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.fmode, |
| 762 | strerror(errno)); |
| 763 | bad++; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) { |
| 768 | if (parsed.capabilities == 0) { |
| 769 | if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) { |
| 770 | // Report failure unless it's ENODATA (attribute not set) |
| 771 | uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n", filename, |
| 772 | parsed.capabilities, strerror(errno)); |
| 773 | bad++; |
| 774 | } |
| 775 | } else { |
| 776 | struct vfs_cap_data cap_data; |
| 777 | memset(&cap_data, 0, sizeof(cap_data)); |
| 778 | cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE; |
| 779 | cap_data.data[0].permitted = (uint32_t)(parsed.capabilities & 0xffffffff); |
| 780 | cap_data.data[0].inheritable = 0; |
| 781 | cap_data.data[1].permitted = (uint32_t)(parsed.capabilities >> 32); |
| 782 | cap_data.data[1].inheritable = 0; |
| 783 | if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) { |
| 784 | uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n", filename, |
| 785 | parsed.capabilities, strerror(errno)); |
| 786 | bad++; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | return bad; |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | // nftw doesn't allow us to pass along context, so we need to use |
| 795 | // global variables. *sigh* |
| 796 | static struct perm_parsed_args recursive_parsed_args; |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 797 | static State* recursive_state; |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 798 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 799 | static int do_SetMetadataRecursive(const char* filename, const struct stat* statptr, int fileflags, |
| 800 | struct FTW* pfwt) { |
| 801 | return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 805 | if ((argc % 2) != 1) { |
| 806 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects an odd number of arguments, got %d", |
| 807 | name, argc); |
| 808 | } |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 809 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 810 | std::vector<std::string> args; |
| 811 | if (!ReadArgs(state, argc, argv, &args)) { |
| 812 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 813 | } |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 814 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 815 | struct stat sb; |
| 816 | if (lstat(args[0].c_str(), &sb) == -1) { |
| 817 | return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s", name, |
| 818 | args[0].c_str(), strerror(errno)); |
| 819 | } |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 820 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 821 | struct perm_parsed_args parsed = ParsePermArgs(state, argc, args); |
| 822 | int bad = 0; |
| 823 | bool recursive = (strcmp(name, "set_metadata_recursive") == 0); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 824 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 825 | if (recursive) { |
| 826 | recursive_parsed_args = parsed; |
| 827 | recursive_state = state; |
| 828 | bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS); |
| 829 | memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args)); |
| 830 | recursive_state = NULL; |
| 831 | } else { |
| 832 | bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed); |
| 833 | } |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 834 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 835 | if (bad > 0) { |
| 836 | return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name); |
| 837 | } |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 838 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 839 | return StringValue(""); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 840 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 841 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 842 | Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 843 | if (argc != 1) { |
| 844 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
| 845 | } |
| 846 | std::string key; |
| 847 | if (!Evaluate(state, argv[0], &key)) { |
| 848 | return nullptr; |
| 849 | } |
| 850 | std::string value = android::base::GetProperty(key, ""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 851 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 852 | return StringValue(value); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 855 | // file_getprop(file, key) |
| 856 | // |
| 857 | // interprets 'file' as a getprop-style file (key=value pairs, one |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 858 | // per line. # comment lines, blank lines, lines without '=' ignored), |
Michael Runge | aa1a31e | 2014-04-25 18:47:18 -0700 | [diff] [blame] | 859 | // and returns the value for 'key' (or "" if it isn't defined). |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 860 | Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 861 | if (argc != 2) { |
| 862 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 863 | } |
| 864 | |
| 865 | std::vector<std::string> args; |
| 866 | if (!ReadArgs(state, 2, argv, &args)) { |
| 867 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 868 | } |
| 869 | const std::string& filename = args[0]; |
| 870 | const std::string& key = args[1]; |
| 871 | |
| 872 | struct stat st; |
| 873 | if (stat(filename.c_str(), &st) < 0) { |
| 874 | return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, |
| 875 | filename.c_str(), strerror(errno)); |
| 876 | } |
| 877 | |
| 878 | constexpr off_t MAX_FILE_GETPROP_SIZE = 65536; |
| 879 | if (st.st_size > MAX_FILE_GETPROP_SIZE) { |
| 880 | return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)", |
| 881 | filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE)); |
| 882 | } |
| 883 | |
| 884 | std::string buffer(st.st_size, '\0'); |
| 885 | unique_file f(ota_fopen(filename.c_str(), "rb")); |
| 886 | if (f == nullptr) { |
| 887 | return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename.c_str(), |
| 888 | strerror(errno)); |
| 889 | } |
| 890 | |
| 891 | if (ota_fread(&buffer[0], 1, st.st_size, f.get()) != static_cast<size_t>(st.st_size)) { |
| 892 | ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", name, |
| 893 | static_cast<size_t>(st.st_size), filename.c_str()); |
| 894 | return nullptr; |
| 895 | } |
| 896 | |
| 897 | ota_fclose(f); |
| 898 | |
| 899 | std::vector<std::string> lines = android::base::Split(buffer, "\n"); |
| 900 | for (size_t i = 0; i < lines.size(); i++) { |
| 901 | std::string line = android::base::Trim(lines[i]); |
| 902 | |
| 903 | // comment or blank line: skip to next line |
| 904 | if (line.empty() || line[0] == '#') { |
| 905 | continue; |
| 906 | } |
| 907 | size_t equal_pos = line.find('='); |
| 908 | if (equal_pos == std::string::npos) { |
| 909 | continue; |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 912 | // trim whitespace between key and '=' |
| 913 | std::string str = android::base::Trim(line.substr(0, equal_pos)); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 914 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 915 | // not the key we're looking for |
| 916 | if (key != str) continue; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 917 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 918 | return StringValue(android::base::Trim(line.substr(equal_pos + 1))); |
| 919 | } |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 920 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 921 | return StringValue(""); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 924 | // apply_patch_space(bytes) |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 925 | Value* ApplyPatchSpaceFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 926 | std::vector<std::string> args; |
| 927 | if (!ReadArgs(state, 1, argv, &args)) { |
| 928 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 929 | } |
| 930 | const std::string& bytes_str = args[0]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 931 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 932 | size_t bytes; |
| 933 | if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) { |
| 934 | return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n", |
| 935 | name, bytes_str.c_str()); |
| 936 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 937 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 938 | return StringValue(CacheSizeCheck(bytes) ? "" : "t"); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 939 | } |
| 940 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 941 | // apply_patch(src_file, tgt_file, tgt_sha1, tgt_size, patch1_sha1, patch1_blob, [...]) |
| 942 | // Applies a binary patch to the src_file to produce the tgt_file. If the desired target is the |
| 943 | // same as the source, pass "-" for tgt_file. tgt_sha1 and tgt_size are the expected final SHA1 |
| 944 | // hash and size of the target file. The remaining arguments must come in pairs: a SHA1 hash (a |
| 945 | // 40-character hex string) and a blob. The blob is the patch to be applied when the source |
| 946 | // file's current contents have the given SHA1. |
| 947 | // |
| 948 | // The patching is done in a safe manner that guarantees the target file either has the desired |
| 949 | // SHA1 hash and size, or it is untouched -- it will not be left in an unrecoverable intermediate |
| 950 | // state. If the process is interrupted during patching, the target file may be in an intermediate |
| 951 | // state; a copy exists in the cache partition so restarting the update can successfully update |
| 952 | // the file. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 953 | Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 954 | if (argc < 6 || (argc % 2) == 1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 955 | return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an " |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 956 | "even number, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 959 | std::vector<std::string> args; |
| 960 | if (!ReadArgs(state, 4, argv, &args)) { |
| 961 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 962 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 963 | const std::string& source_filename = args[0]; |
| 964 | const std::string& target_filename = args[1]; |
| 965 | const std::string& target_sha1 = args[2]; |
| 966 | const std::string& target_size_str = args[3]; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 967 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 968 | size_t target_size; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 969 | if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) { |
| 970 | return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", |
| 971 | name, target_size_str.c_str()); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | int patchcount = (argc-4) / 2; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 975 | std::vector<std::unique_ptr<Value>> arg_values; |
| 976 | if (!ReadValueArgs(state, argc-4, argv+4, &arg_values)) { |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 977 | return nullptr; |
| 978 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 979 | |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 980 | for (int i = 0; i < patchcount; ++i) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 981 | if (arg_values[i * 2]->type != VAL_STRING) { |
| 982 | return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, |
| 983 | i * 2); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 984 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 985 | if (arg_values[i * 2 + 1]->type != VAL_BLOB) { |
| 986 | return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, |
| 987 | i * 2 + 1); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 988 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 989 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 990 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 991 | std::vector<std::string> patch_sha_str; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 992 | std::vector<std::unique_ptr<Value>> patches; |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 993 | for (int i = 0; i < patchcount; ++i) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 994 | patch_sha_str.push_back(arg_values[i * 2]->data); |
| 995 | patches.push_back(std::move(arg_values[i * 2 + 1])); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 996 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 997 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 998 | int result = applypatch(source_filename.c_str(), target_filename.c_str(), |
| 999 | target_sha1.c_str(), target_size, |
| 1000 | patch_sha_str, patches, nullptr); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1001 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1002 | return StringValue(result == 0 ? "t" : ""); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1005 | // apply_patch_check(filename, [sha1, ...]) |
| 1006 | // Returns true if the contents of filename or the temporary copy in the cache partition (if |
| 1007 | // present) have a SHA-1 checksum equal to one of the given sha1 values. sha1 values are |
| 1008 | // specified as 40 hex digits. This function differs from sha1_check(read_file(filename), |
| 1009 | // sha1 [, ...]) in that it knows to check the cache partition copy, so apply_patch_check() will |
| 1010 | // succeed even if the file was corrupted by an interrupted apply_patch() update. |
| 1011 | Value* ApplyPatchCheckFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1012 | if (argc < 1) { |
| 1013 | return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d", name, |
| 1014 | argc); |
| 1015 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1016 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1017 | std::vector<std::string> args; |
| 1018 | if (!ReadArgs(state, 1, argv, &args)) { |
| 1019 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 1020 | } |
| 1021 | const std::string& filename = args[0]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1022 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1023 | std::vector<std::string> sha1s; |
| 1024 | if (!ReadArgs(state, argc - 1, argv + 1, &sha1s)) { |
| 1025 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 1026 | } |
| 1027 | int result = applypatch_check(filename.c_str(), sha1s); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1028 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1029 | return StringValue(result == 0 ? "t" : ""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 1032 | // This is the updater side handler for ui_print() in edify script. Contents |
| 1033 | // will be sent over to the recovery side for on-screen display. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1034 | Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1035 | std::vector<std::string> args; |
| 1036 | if (!ReadArgs(state, argc, argv, &args)) { |
| 1037 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 1038 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1039 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1040 | std::string buffer = android::base::Join(args, "") + "\n"; |
| 1041 | uiPrint(state, buffer); |
| 1042 | return StringValue(buffer); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1045 | Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1046 | if (argc != 0) { |
| 1047 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc); |
| 1048 | } |
| 1049 | fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n"); |
| 1050 | return StringValue("t"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1053 | Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1054 | if (argc < 1) { |
| 1055 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name); |
| 1056 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1057 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1058 | std::vector<std::string> args; |
| 1059 | if (!ReadArgs(state, argc, argv, &args)) { |
| 1060 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 1061 | } |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1062 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1063 | char* args2[argc + 1]; |
| 1064 | for (int i = 0; i < argc; i++) { |
| 1065 | args2[i] = &args[i][0]; |
| 1066 | } |
| 1067 | args2[argc] = nullptr; |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1068 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1069 | LOG(INFO) << "about to run program [" << args2[0] << "] with " << argc << " args"; |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1070 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1071 | pid_t child = fork(); |
| 1072 | if (child == 0) { |
| 1073 | execv(args2[0], args2); |
| 1074 | PLOG(ERROR) << "run_program: execv failed"; |
| 1075 | _exit(1); |
| 1076 | } |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1077 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1078 | int status; |
| 1079 | waitpid(child, &status, 0); |
| 1080 | if (WIFEXITED(status)) { |
| 1081 | if (WEXITSTATUS(status) != 0) { |
| 1082 | LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status); |
| 1083 | } |
| 1084 | } else if (WIFSIGNALED(status)) { |
| 1085 | LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status); |
| 1086 | } |
| 1087 | |
| 1088 | return StringValue(std::to_string(status)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1091 | // sha1_check(data) |
| 1092 | // to return the sha1 of the data (given in the format returned by |
| 1093 | // read_file). |
| 1094 | // |
| 1095 | // sha1_check(data, sha1_hex, [sha1_hex, ...]) |
| 1096 | // returns the sha1 of the file if it matches any of the hex |
| 1097 | // strings passed, or "" if it does not equal any of them. |
| 1098 | // |
| 1099 | Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1100 | if (argc < 1) { |
| 1101 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name); |
| 1102 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1103 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1104 | std::vector<std::unique_ptr<Value>> args; |
| 1105 | if (!ReadValueArgs(state, argc, argv, &args)) { |
| 1106 | return nullptr; |
| 1107 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1108 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1109 | if (args[0]->type == VAL_INVALID) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1110 | return StringValue(""); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1111 | } |
| 1112 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 1113 | SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest); |
| 1114 | |
| 1115 | if (argc == 1) { |
| 1116 | return StringValue(print_sha1(digest)); |
| 1117 | } |
| 1118 | |
| 1119 | for (int i = 1; i < argc; ++i) { |
| 1120 | uint8_t arg_digest[SHA_DIGEST_LENGTH]; |
| 1121 | if (args[i]->type != VAL_STRING) { |
| 1122 | LOG(ERROR) << name << "(): arg " << i << " is not a string; skipping"; |
| 1123 | } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) { |
| 1124 | // Warn about bad args and skip them. |
| 1125 | LOG(ERROR) << name << "(): error parsing \"" << args[i]->data << "\" as sha-1; skipping"; |
| 1126 | } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) { |
| 1127 | // Found a match. |
| 1128 | return args[i].release(); |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | // Didn't match any of the hex strings; return false. |
| 1133 | return StringValue(""); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1134 | } |
| 1135 | |
Hristo Bojinov | db314d6 | 2010-08-02 10:29:49 -0700 | [diff] [blame] | 1136 | // Read a local file and return its contents (the Value* returned |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1137 | // is actually a FileContents*). |
| 1138 | Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1139 | if (argc != 1) { |
| 1140 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
| 1141 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1142 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1143 | std::vector<std::string> args; |
| 1144 | if (!ReadArgs(state, 1, argv, &args)) { |
| 1145 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 1146 | } |
| 1147 | const std::string& filename = args[0]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1148 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1149 | Value* v = new Value(VAL_INVALID, ""); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1150 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1151 | FileContents fc; |
| 1152 | if (LoadFileContents(filename.c_str(), &fc) == 0) { |
| 1153 | v->type = VAL_BLOB; |
| 1154 | v->data = std::string(fc.data.begin(), fc.data.end()); |
| 1155 | } |
| 1156 | return v; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1157 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1158 | |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 1159 | // write_value(value, filename) |
| 1160 | // Writes 'value' to 'filename'. |
| 1161 | // Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq") |
| 1162 | Value* WriteValueFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1163 | if (argc != 2) { |
| 1164 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 1165 | } |
| 1166 | |
| 1167 | std::vector<std::string> args; |
| 1168 | if (!ReadArgs(state, 2, argv, &args)) { |
| 1169 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
| 1170 | } |
| 1171 | |
| 1172 | const std::string& filename = args[1]; |
| 1173 | if (filename.empty()) { |
| 1174 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name); |
| 1175 | } |
| 1176 | |
| 1177 | const std::string& value = args[0]; |
| 1178 | if (!android::base::WriteStringToFile(value, filename)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1179 | PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\""; |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 1180 | return StringValue(""); |
| 1181 | } else { |
| 1182 | return StringValue("t"); |
| 1183 | } |
| 1184 | } |
| 1185 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1186 | // Immediately reboot the device. Recovery is not finished normally, |
| 1187 | // so if you reboot into recovery it will re-start applying the |
| 1188 | // current package (because nothing has cleared the copy of the |
| 1189 | // arguments stored in the BCB). |
| 1190 | // |
| 1191 | // The argument is the partition name passed to the android reboot |
| 1192 | // property. It can be "recovery" to boot from the recovery |
| 1193 | // partition, or "" (empty string) to boot from the regular boot |
| 1194 | // partition. |
| 1195 | Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1196 | if (argc != 2) { |
| 1197 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 1198 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1199 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1200 | std::vector<std::string> args; |
| 1201 | if (!ReadArgs(state, 2, argv, &args)) { |
| 1202 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
| 1203 | } |
| 1204 | const std::string& filename = args[0]; |
| 1205 | const std::string& property = args[1]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1206 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1207 | // Zero out the 'command' field of the bootloader message. Leave the rest intact. |
| 1208 | bootloader_message boot; |
| 1209 | std::string err; |
| 1210 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1211 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1212 | return StringValue(""); |
| 1213 | } |
| 1214 | memset(boot.command, 0, sizeof(boot.command)); |
| 1215 | if (!write_bootloader_message_to(boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1216 | LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1217 | return StringValue(""); |
| 1218 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1219 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1220 | const std::string reboot_cmd = "reboot," + property; |
| 1221 | android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1222 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1223 | sleep(5); |
| 1224 | return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | // Store a string value somewhere that future invocations of recovery |
| 1228 | // can access it. This value is called the "stage" and can be used to |
| 1229 | // drive packages that need to do reboots in the middle of |
| 1230 | // installation and keep track of where they are in the multi-stage |
| 1231 | // install. |
| 1232 | // |
| 1233 | // The first argument is the block device for the misc partition |
| 1234 | // ("/misc" in the fstab), which is where this value is stored. The |
| 1235 | // second argument is the string to store; it should not exceed 31 |
| 1236 | // bytes. |
| 1237 | Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1238 | if (argc != 2) { |
| 1239 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 1240 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1241 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1242 | std::vector<std::string> args; |
| 1243 | if (!ReadArgs(state, 2, argv, &args)) { |
| 1244 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 1245 | } |
| 1246 | const std::string& filename = args[0]; |
| 1247 | const std::string& stagestr = args[1]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1248 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1249 | // Store this value in the misc partition, immediately after the |
| 1250 | // bootloader message that the main recovery uses to save its |
| 1251 | // arguments in case of the device restarting midway through |
| 1252 | // package installation. |
| 1253 | bootloader_message boot; |
| 1254 | std::string err; |
| 1255 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1256 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1257 | return StringValue(""); |
| 1258 | } |
| 1259 | strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage)); |
| 1260 | if (!write_bootloader_message_to(boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1261 | LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1262 | return StringValue(""); |
| 1263 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1264 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1265 | return StringValue(filename); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | // Return the value most recently saved with SetStageFn. The argument |
| 1269 | // is the block device for the misc partition. |
| 1270 | Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1271 | if (argc != 1) { |
| 1272 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
| 1273 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1274 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1275 | std::vector<std::string> args; |
| 1276 | if (!ReadArgs(state, 1, argv, &args)) { |
| 1277 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 1278 | } |
| 1279 | const std::string& filename = args[0]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1280 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1281 | bootloader_message boot; |
| 1282 | std::string err; |
| 1283 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1284 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1285 | return StringValue(""); |
| 1286 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1287 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 1288 | return StringValue(boot.stage); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1289 | } |
| 1290 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1291 | Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 1292 | if (argc != 2) { |
| 1293 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 1294 | } |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1295 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 1296 | std::vector<std::string> args; |
| 1297 | if (!ReadArgs(state, 2, argv, &args)) { |
| 1298 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 1299 | } |
| 1300 | const std::string& filename = args[0]; |
| 1301 | const std::string& len_str = args[1]; |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1302 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 1303 | size_t len; |
| 1304 | if (!android::base::ParseUint(len_str.c_str(), &len)) { |
| 1305 | return nullptr; |
| 1306 | } |
| 1307 | unique_fd fd(ota_open(filename.c_str(), O_WRONLY, 0644)); |
| 1308 | // The wipe_block_device function in ext4_utils returns 0 on success and 1 |
| 1309 | // for failure. |
| 1310 | int status = wipe_block_device(fd, len); |
| 1311 | return StringValue((status == 0) ? "t" : ""); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1312 | } |
| 1313 | |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 1314 | Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1315 | if (argc != 0) { |
| 1316 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc); |
| 1317 | } |
| 1318 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 1319 | fprintf(ui->cmd_pipe, "enable_reboot\n"); |
| 1320 | return StringValue("t"); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1323 | Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1324 | if (argc == 0) { |
| 1325 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc); |
| 1326 | } |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1327 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1328 | std::vector<std::string> args; |
| 1329 | if (!ReadArgs(state, argc, argv, &args)) { |
| 1330 | return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name); |
| 1331 | } |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1332 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1333 | char* args2[argc + 1]; |
| 1334 | // Tune2fs expects the program name as its args[0] |
| 1335 | args2[0] = const_cast<char*>(name); |
| 1336 | if (args2[0] == nullptr) { |
| 1337 | return nullptr; |
| 1338 | } |
| 1339 | for (int i = 0; i < argc; ++i) { |
| 1340 | args2[i + 1] = &args[i][0]; |
| 1341 | } |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1342 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1343 | // tune2fs changes the file system parameters on an ext2 file system; it |
| 1344 | // returns 0 on success. |
| 1345 | int result = tune2fs_main(argc + 1, args2); |
| 1346 | if (result != 0) { |
| 1347 | return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result); |
| 1348 | } |
| 1349 | return StringValue("t"); |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1350 | } |
| 1351 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1352 | void RegisterInstallFunctions() { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1353 | RegisterFunction("mount", MountFn); |
| 1354 | RegisterFunction("is_mounted", IsMountedFn); |
| 1355 | RegisterFunction("unmount", UnmountFn); |
| 1356 | RegisterFunction("format", FormatFn); |
| 1357 | RegisterFunction("show_progress", ShowProgressFn); |
| 1358 | RegisterFunction("set_progress", SetProgressFn); |
| 1359 | RegisterFunction("delete", DeleteFn); |
| 1360 | RegisterFunction("delete_recursive", DeleteFn); |
| 1361 | RegisterFunction("package_extract_dir", PackageExtractDirFn); |
| 1362 | RegisterFunction("package_extract_file", PackageExtractFileFn); |
| 1363 | RegisterFunction("symlink", SymlinkFn); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1364 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1365 | // Usage: |
| 1366 | // set_metadata("filename", "key1", "value1", "key2", "value2", ...) |
| 1367 | // Example: |
| 1368 | // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", |
| 1369 | // "u:object_r:system_file:s0", "capabilities", 0x0); |
| 1370 | RegisterFunction("set_metadata", SetMetadataFn); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1371 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1372 | // Usage: |
| 1373 | // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...) |
| 1374 | // Example: |
| 1375 | // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, |
| 1376 | // "selabel", "u:object_r:system_file:s0", "capabilities", 0x0); |
| 1377 | RegisterFunction("set_metadata_recursive", SetMetadataFn); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1378 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1379 | RegisterFunction("getprop", GetPropFn); |
| 1380 | RegisterFunction("file_getprop", FileGetPropFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1381 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1382 | RegisterFunction("apply_patch", ApplyPatchFn); |
| 1383 | RegisterFunction("apply_patch_check", ApplyPatchCheckFn); |
| 1384 | RegisterFunction("apply_patch_space", ApplyPatchSpaceFn); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1385 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1386 | RegisterFunction("wipe_block_device", WipeBlockDeviceFn); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1387 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1388 | RegisterFunction("read_file", ReadFileFn); |
| 1389 | RegisterFunction("sha1_check", Sha1CheckFn); |
| 1390 | RegisterFunction("rename", RenameFn); |
| 1391 | RegisterFunction("write_value", WriteValueFn); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1392 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1393 | RegisterFunction("wipe_cache", WipeCacheFn); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1394 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1395 | RegisterFunction("ui_print", UIPrintFn); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1396 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1397 | RegisterFunction("run_program", RunProgramFn); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1398 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1399 | RegisterFunction("reboot_now", RebootNowFn); |
| 1400 | RegisterFunction("get_stage", GetStageFn); |
| 1401 | RegisterFunction("set_stage", SetStageFn); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 1402 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1403 | RegisterFunction("enable_reboot", EnableRebootFn); |
| 1404 | RegisterFunction("tune2fs", Tune2FsFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1405 | } |