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