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