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