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