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