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