Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 17 | #include "updater/install.h" |
| 18 | |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 19 | #include <ctype.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 20 | #include <errno.h> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
| 22 | #include <ftw.h> |
| 23 | #include <inttypes.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 24 | #include <stdarg.h> |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 25 | #include <stdio.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 28 | #include <sys/capability.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 29 | #include <sys/mount.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 32 | #include <sys/wait.h> |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 33 | #include <sys/xattr.h> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 34 | #include <time.h> |
| 35 | #include <unistd.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 36 | #include <utime.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 37 | |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 38 | #include <memory> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 39 | #include <string> |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 40 | #include <vector> |
| 41 | |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 42 | #include <android-base/file.h> |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 43 | #include <android-base/logging.h> |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 44 | #include <android-base/parsedouble.h> |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 45 | #include <android-base/parseint.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 46 | #include <android-base/properties.h> |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 47 | #include <android-base/stringprintf.h> |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 48 | #include <android-base/strings.h> |
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 | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 51 | #include <ext4_utils/wipe.h> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 52 | #include <openssl/sha.h> |
Elliott Hughes | 4bbd5bf | 2016-04-01 18:24:39 -0700 | [diff] [blame] | 53 | #include <selinux/label.h> |
| 54 | #include <selinux/selinux.h> |
Tao Bao | 09e468f | 2017-09-29 14:39:33 -0700 | [diff] [blame] | 55 | #include <tune2fs.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 56 | #include <ziparchive/zip_archive.h> |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 57 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 58 | #include "edify/expr.h" |
Tao Bao | d33b2f8 | 2017-09-28 21:29:11 -0700 | [diff] [blame] | 59 | #include "otafault/ota_io.h" |
Tao Bao | 17054c0 | 2018-05-03 22:41:23 -0700 | [diff] [blame] | 60 | #include "otautil/dirutil.h" |
Tao Bao | 1fc5bf3 | 2017-10-06 07:43:41 -0700 | [diff] [blame] | 61 | #include "otautil/error_code.h" |
Tao Bao | 9a319f0 | 2018-01-04 13:19:11 -0800 | [diff] [blame] | 62 | #include "otautil/mounts.h" |
Tao Bao | 09e468f | 2017-09-29 14:39:33 -0700 | [diff] [blame] | 63 | #include "otautil/print_sha1.h" |
Tao Bao | 2c52639 | 2018-05-03 23:01:13 -0700 | [diff] [blame] | 64 | #include "otautil/sysutil.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 65 | #include "updater/updater.h" |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 66 | |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 67 | // Send over the buffer to recovery though the command pipe. |
| 68 | static void uiPrint(State* state, const std::string& buffer) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 69 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 70 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 71 | // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "". |
| 72 | // So skip sending empty strings to UI. |
| 73 | std::vector<std::string> lines = android::base::Split(buffer, "\n"); |
| 74 | for (auto& line : lines) { |
| 75 | if (!line.empty()) { |
| 76 | fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str()); |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 77 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 78 | } |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 79 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 80 | // On the updater side, we need to dump the contents to stderr (which has |
| 81 | // been redirected to the log file). Because the recovery will only print |
| 82 | // the contents to screen when processing pipe command ui_print. |
| 83 | LOG(INFO) << buffer; |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Elliott Hughes | 83ce755 | 2016-06-30 09:28:42 -0700 | [diff] [blame] | 86 | void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 87 | std::string error_msg; |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 88 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 89 | va_list ap; |
| 90 | va_start(ap, format); |
| 91 | android::base::StringAppendV(&error_msg, format, ap); |
| 92 | va_end(ap); |
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 | uiPrint(state, error_msg); |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Tianjie Xu | 5419ad3 | 2018-02-02 16:49:15 -0800 | [diff] [blame] | 97 | // This is the updater side handler for ui_print() in edify script. Contents will be sent over to |
| 98 | // the recovery side for on-screen display. |
| 99 | Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 100 | std::vector<std::string> args; |
| 101 | if (!ReadArgs(state, argv, &args)) { |
| 102 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
| 103 | } |
| 104 | |
| 105 | std::string buffer = android::base::Join(args, ""); |
| 106 | uiPrint(state, buffer); |
| 107 | return StringValue(buffer); |
| 108 | } |
| 109 | |
| 110 | // package_extract_file(package_file[, dest_file]) |
| 111 | // Extracts a single package_file from the update package and writes it to dest_file, |
| 112 | // overwriting existing files if necessary. Without the dest_file argument, returns the |
| 113 | // contents of the package file as a binary blob. |
| 114 | Value* PackageExtractFileFn(const char* name, State* state, |
| 115 | const std::vector<std::unique_ptr<Expr>>& argv) { |
| 116 | if (argv.size() < 1 || argv.size() > 2) { |
| 117 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name, |
| 118 | argv.size()); |
| 119 | } |
| 120 | |
| 121 | if (argv.size() == 2) { |
| 122 | // The two-argument version extracts to a file. |
| 123 | |
| 124 | std::vector<std::string> args; |
| 125 | if (!ReadArgs(state, argv, &args)) { |
| 126 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name, |
| 127 | argv.size()); |
| 128 | } |
| 129 | const std::string& zip_path = args[0]; |
| 130 | const std::string& dest_path = args[1]; |
| 131 | |
| 132 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
| 133 | ZipString zip_string_path(zip_path.c_str()); |
| 134 | ZipEntry entry; |
| 135 | if (FindEntry(za, zip_string_path, &entry) != 0) { |
| 136 | LOG(ERROR) << name << ": no " << zip_path << " in package"; |
| 137 | return StringValue(""); |
| 138 | } |
| 139 | |
| 140 | unique_fd fd(TEMP_FAILURE_RETRY( |
| 141 | ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR))); |
| 142 | if (fd == -1) { |
| 143 | PLOG(ERROR) << name << ": can't open " << dest_path << " for write"; |
| 144 | return StringValue(""); |
| 145 | } |
| 146 | |
| 147 | bool success = true; |
| 148 | int32_t ret = ExtractEntryToFile(za, &entry, fd); |
| 149 | if (ret != 0) { |
| 150 | LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" (" |
| 151 | << entry.uncompressed_length << " bytes) to \"" << dest_path |
| 152 | << "\": " << ErrorCodeString(ret); |
| 153 | success = false; |
| 154 | } |
| 155 | if (ota_fsync(fd) == -1) { |
| 156 | PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed"; |
| 157 | success = false; |
| 158 | } |
| 159 | if (ota_close(fd) == -1) { |
| 160 | PLOG(ERROR) << "close of \"" << dest_path << "\" failed"; |
| 161 | success = false; |
| 162 | } |
| 163 | |
| 164 | return StringValue(success ? "t" : ""); |
| 165 | } else { |
| 166 | // The one-argument version returns the contents of the file as the result. |
| 167 | |
| 168 | std::vector<std::string> args; |
| 169 | if (!ReadArgs(state, argv, &args)) { |
| 170 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name, |
| 171 | argv.size()); |
| 172 | } |
| 173 | const std::string& zip_path = args[0]; |
| 174 | |
| 175 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
| 176 | ZipString zip_string_path(zip_path.c_str()); |
| 177 | ZipEntry entry; |
| 178 | if (FindEntry(za, zip_string_path, &entry) != 0) { |
| 179 | return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name, |
| 180 | zip_path.c_str()); |
| 181 | } |
| 182 | |
| 183 | std::string buffer; |
| 184 | buffer.resize(entry.uncompressed_length); |
| 185 | |
| 186 | int32_t ret = |
| 187 | ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size()); |
| 188 | if (ret != 0) { |
| 189 | return ErrorAbort(state, kPackageExtractFileFailure, |
| 190 | "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name, |
| 191 | zip_path.c_str(), buffer.size(), ErrorCodeString(ret)); |
| 192 | } |
| 193 | |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 194 | return new Value(Value::Type::BLOB, buffer); |
Tianjie Xu | 5419ad3 | 2018-02-02 16:49:15 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | // apply_patch(src_file, tgt_file, tgt_sha1, tgt_size, patch1_sha1, patch1_blob, [...]) |
| 199 | // Applies a binary patch to the src_file to produce the tgt_file. If the desired target is the |
| 200 | // same as the source, pass "-" for tgt_file. tgt_sha1 and tgt_size are the expected final SHA1 |
| 201 | // hash and size of the target file. The remaining arguments must come in pairs: a SHA1 hash (a |
| 202 | // 40-character hex string) and a blob. The blob is the patch to be applied when the source |
| 203 | // file's current contents have the given SHA1. |
| 204 | // |
| 205 | // The patching is done in a safe manner that guarantees the target file either has the desired |
| 206 | // SHA1 hash and size, or it is untouched -- it will not be left in an unrecoverable intermediate |
| 207 | // state. If the process is interrupted during patching, the target file may be in an intermediate |
| 208 | // state; a copy exists in the cache partition so restarting the update can successfully update |
| 209 | // the file. |
| 210 | Value* ApplyPatchFn(const char* name, State* state, |
| 211 | const std::vector<std::unique_ptr<Expr>>& argv) { |
| 212 | if (argv.size() < 6 || (argv.size() % 2) == 1) { |
| 213 | return ErrorAbort(state, kArgsParsingFailure, |
| 214 | "%s(): expected at least 6 args and an " |
| 215 | "even number, got %zu", |
| 216 | name, argv.size()); |
| 217 | } |
| 218 | |
| 219 | std::vector<std::string> args; |
| 220 | if (!ReadArgs(state, argv, &args, 0, 4)) { |
| 221 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 222 | } |
| 223 | const std::string& source_filename = args[0]; |
| 224 | const std::string& target_filename = args[1]; |
| 225 | const std::string& target_sha1 = args[2]; |
| 226 | const std::string& target_size_str = args[3]; |
| 227 | |
| 228 | size_t target_size; |
| 229 | if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) { |
| 230 | return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name, |
| 231 | target_size_str.c_str()); |
| 232 | } |
| 233 | |
| 234 | int patchcount = (argv.size() - 4) / 2; |
| 235 | std::vector<std::unique_ptr<Value>> arg_values; |
| 236 | if (!ReadValueArgs(state, argv, &arg_values, 4, argv.size() - 4)) { |
| 237 | return nullptr; |
| 238 | } |
| 239 | |
| 240 | for (int i = 0; i < patchcount; ++i) { |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 241 | if (arg_values[i * 2]->type != Value::Type::STRING) { |
Tianjie Xu | 5419ad3 | 2018-02-02 16:49:15 -0800 | [diff] [blame] | 242 | return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, i * 2); |
| 243 | } |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 244 | if (arg_values[i * 2 + 1]->type != Value::Type::BLOB) { |
Tianjie Xu | 5419ad3 | 2018-02-02 16:49:15 -0800 | [diff] [blame] | 245 | return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, i * 2 + 1); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | std::vector<std::string> patch_sha_str; |
| 250 | std::vector<std::unique_ptr<Value>> patches; |
| 251 | for (int i = 0; i < patchcount; ++i) { |
| 252 | patch_sha_str.push_back(arg_values[i * 2]->data); |
| 253 | patches.push_back(std::move(arg_values[i * 2 + 1])); |
| 254 | } |
| 255 | |
| 256 | int result = applypatch(source_filename.c_str(), target_filename.c_str(), target_sha1.c_str(), |
| 257 | target_size, patch_sha_str, patches, nullptr); |
| 258 | |
| 259 | return StringValue(result == 0 ? "t" : ""); |
| 260 | } |
| 261 | |
| 262 | // apply_patch_check(filename, [sha1, ...]) |
| 263 | // Returns true if the contents of filename or the temporary copy in the cache partition (if |
| 264 | // present) have a SHA-1 checksum equal to one of the given sha1 values. sha1 values are |
Tao Bao | 0b58e9a | 2018-07-06 15:01:05 -0700 | [diff] [blame] | 265 | // specified as 40 hex digits. |
Tianjie Xu | 5419ad3 | 2018-02-02 16:49:15 -0800 | [diff] [blame] | 266 | Value* ApplyPatchCheckFn(const char* name, State* state, |
| 267 | const std::vector<std::unique_ptr<Expr>>& argv) { |
| 268 | if (argv.size() < 1) { |
| 269 | return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %zu", name, |
| 270 | argv.size()); |
| 271 | } |
| 272 | |
| 273 | std::vector<std::string> args; |
| 274 | if (!ReadArgs(state, argv, &args, 0, 1)) { |
| 275 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 276 | } |
| 277 | const std::string& filename = args[0]; |
| 278 | |
| 279 | std::vector<std::string> sha1s; |
| 280 | if (argv.size() > 1 && !ReadArgs(state, argv, &sha1s, 1, argv.size() - 1)) { |
| 281 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 282 | } |
| 283 | int result = applypatch_check(filename.c_str(), sha1s); |
| 284 | |
| 285 | return StringValue(result == 0 ? "t" : ""); |
| 286 | } |
| 287 | |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 288 | // mount(fs_type, partition_type, location, mount_point) |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 289 | // mount(fs_type, partition_type, location, mount_point, mount_options) |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 290 | |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 291 | // fs_type="ext4" partition_type="EMMC" location=device |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 292 | Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 293 | if (argv.size() != 4 && argv.size() != 5) { |
| 294 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name, |
| 295 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 299 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 300 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 301 | } |
| 302 | const std::string& fs_type = args[0]; |
| 303 | const std::string& partition_type = args[1]; |
| 304 | const std::string& location = args[2]; |
| 305 | const std::string& mount_point = args[3]; |
| 306 | std::string mount_options; |
| 307 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 308 | if (argv.size() == 5) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 309 | mount_options = args[4]; |
| 310 | } |
| 311 | |
| 312 | if (fs_type.empty()) { |
| 313 | return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name); |
| 314 | } |
| 315 | if (partition_type.empty()) { |
| 316 | return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty", |
| 317 | name); |
| 318 | } |
| 319 | if (location.empty()) { |
| 320 | return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name); |
| 321 | } |
| 322 | if (mount_point.empty()) { |
| 323 | return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty", |
| 324 | name); |
| 325 | } |
| 326 | |
| 327 | { |
| 328 | char* secontext = nullptr; |
| 329 | |
| 330 | if (sehandle) { |
| 331 | selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755); |
| 332 | setfscreatecon(secontext); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 333 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 334 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 335 | mkdir(mount_point.c_str(), 0755); |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 336 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 337 | if (secontext) { |
| 338 | freecon(secontext); |
| 339 | setfscreatecon(nullptr); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 340 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 341 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 342 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 343 | if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(), |
| 344 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) { |
Tao Bao | 0bbc764 | 2017-03-29 23:57:47 -0700 | [diff] [blame] | 345 | uiPrintf(state, "%s: Failed to mount %s at %s: %s", name, location.c_str(), mount_point.c_str(), |
| 346 | strerror(errno)); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 347 | return StringValue(""); |
| 348 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 349 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 350 | return StringValue(mount_point); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 353 | // is_mounted(mount_point) |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 354 | Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 355 | if (argv.size() != 1) { |
| 356 | 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] | 357 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 358 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 359 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 360 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 361 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 362 | } |
| 363 | const std::string& mount_point = args[0]; |
| 364 | if (mount_point.empty()) { |
| 365 | return ErrorAbort(state, kArgsParsingFailure, |
| 366 | "mount_point argument to unmount() can't be empty"); |
| 367 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 368 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 369 | scan_mounted_volumes(); |
| 370 | MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str()); |
| 371 | if (vol == nullptr) { |
| 372 | return StringValue(""); |
| 373 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 374 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 375 | return StringValue(mount_point); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 378 | Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 379 | if (argv.size() != 1) { |
| 380 | 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] | 381 | } |
| 382 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 383 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 384 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 385 | } |
| 386 | const std::string& mount_point = args[0]; |
| 387 | if (mount_point.empty()) { |
| 388 | return ErrorAbort(state, kArgsParsingFailure, |
| 389 | "mount_point argument to unmount() can't be empty"); |
| 390 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 391 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 392 | scan_mounted_volumes(); |
| 393 | MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str()); |
| 394 | if (vol == nullptr) { |
Tao Bao | 0bbc764 | 2017-03-29 23:57:47 -0700 | [diff] [blame] | 395 | 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] | 396 | return nullptr; |
| 397 | } else { |
| 398 | int ret = unmount_mounted_volume(vol); |
| 399 | if (ret != 0) { |
Tao Bao | 0bbc764 | 2017-03-29 23:57:47 -0700 | [diff] [blame] | 400 | uiPrintf(state, "Failed to unmount %s: %s", mount_point.c_str(), strerror(errno)); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 401 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 402 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 403 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 404 | return StringValue(mount_point); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 405 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 406 | |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 407 | static int exec_cmd(const char* path, char* const argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 408 | pid_t child; |
| 409 | if ((child = vfork()) == 0) { |
| 410 | execv(path, argv); |
Tao Bao | 3da8801 | 2017-02-03 13:09:23 -0800 | [diff] [blame] | 411 | _exit(EXIT_FAILURE); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | int status; |
| 415 | waitpid(child, &status, 0); |
| 416 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 417 | LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status); |
| 418 | } |
| 419 | return WEXITSTATUS(status); |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 422 | // format(fs_type, partition_type, location, fs_size, mount_point) |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 423 | // |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 424 | // fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location> |
| 425 | // 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] | 426 | // if fs_size == 0, then make fs uses the entire partition. |
Ken Sumrall | 8f132ed | 2011-01-14 18:55:05 -0800 | [diff] [blame] | 427 | // if fs_size > 0, that is the size to use |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 428 | // 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] | 429 | Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 430 | if (argv.size() != 5) { |
| 431 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name, |
| 432 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 433 | } |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 434 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 435 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 436 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 437 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 438 | } |
| 439 | const std::string& fs_type = args[0]; |
| 440 | const std::string& partition_type = args[1]; |
| 441 | const std::string& location = args[2]; |
| 442 | const std::string& fs_size = args[3]; |
| 443 | const std::string& mount_point = args[4]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 444 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 445 | if (fs_type.empty()) { |
| 446 | return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name); |
| 447 | } |
| 448 | if (partition_type.empty()) { |
| 449 | return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty", |
| 450 | name); |
| 451 | } |
| 452 | if (location.empty()) { |
| 453 | return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name); |
| 454 | } |
| 455 | if (mount_point.empty()) { |
| 456 | return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty", |
| 457 | name); |
| 458 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 459 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 460 | int64_t size; |
| 461 | if (!android::base::ParseInt(fs_size, &size)) { |
Tianjie Xu | 5ad8028 | 2018-01-28 15:37:48 -0800 | [diff] [blame] | 462 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name, |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 463 | fs_size.c_str()); |
| 464 | } |
| 465 | |
| 466 | if (fs_type == "ext4") { |
Jiyong Park | 69364fe | 2018-06-20 14:18:18 +0900 | [diff] [blame] | 467 | const char* mke2fs_argv[] = { "/system/bin/mke2fs", "-t", "ext4", "-b", "4096", |
| 468 | location.c_str(), nullptr, nullptr }; |
Jin Qian | ded2dac | 2017-04-21 14:36:12 -0700 | [diff] [blame] | 469 | std::string size_str; |
| 470 | if (size != 0) { |
| 471 | size_str = std::to_string(size / 4096LL); |
| 472 | mke2fs_argv[6] = size_str.c_str(); |
| 473 | } |
| 474 | |
| 475 | int status = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv)); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 476 | if (status != 0) { |
Jin Qian | 502fd1c | 2017-11-02 11:58:12 -0700 | [diff] [blame] | 477 | LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location; |
| 478 | return StringValue(""); |
Jin Qian | ded2dac | 2017-04-21 14:36:12 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Jiyong Park | 69364fe | 2018-06-20 14:18:18 +0900 | [diff] [blame] | 481 | const char* e2fsdroid_argv[] = { "/system/bin/e2fsdroid", "-e", "-a", mount_point.c_str(), |
| 482 | location.c_str(), nullptr }; |
Jin Qian | ded2dac | 2017-04-21 14:36:12 -0700 | [diff] [blame] | 483 | status = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv)); |
| 484 | if (status != 0) { |
| 485 | LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 486 | return StringValue(""); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 487 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 488 | return StringValue(location); |
| 489 | } else if (fs_type == "f2fs") { |
| 490 | if (size < 0) { |
| 491 | LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size; |
| 492 | return StringValue(""); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 493 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 494 | std::string num_sectors = std::to_string(size / 512); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 495 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 496 | const char* f2fs_path = "/sbin/mkfs.f2fs"; |
Jaegeuk Kim | 1a8bb0f | 2017-11-01 13:24:43 -0700 | [diff] [blame] | 497 | const char* f2fs_argv[] = { "mkfs.f2fs", |
| 498 | "-d1", |
| 499 | "-f", |
Jaegeuk Kim | 2e5dc84 | 2018-04-05 22:42:13 -0700 | [diff] [blame] | 500 | "-O", "encrypt", |
| 501 | "-O", "quota", |
| 502 | "-O", "verity", |
| 503 | "-w", "512", |
Jaegeuk Kim | 1a8bb0f | 2017-11-01 13:24:43 -0700 | [diff] [blame] | 504 | location.c_str(), |
| 505 | (size < 512) ? nullptr : num_sectors.c_str(), |
| 506 | nullptr }; |
Tao Bao | 397a813 | 2017-05-12 11:02:27 -0700 | [diff] [blame] | 507 | int status = exec_cmd(f2fs_path, const_cast<char**>(f2fs_argv)); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 508 | if (status != 0) { |
| 509 | LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location; |
| 510 | return StringValue(""); |
| 511 | } |
Jaegeuk Kim | c1c7311 | 2017-11-28 19:48:05 -0800 | [diff] [blame] | 512 | |
| 513 | const char* sload_argv[] = { "/sbin/sload.f2fs", "-t", mount_point.c_str(), location.c_str(), |
| 514 | nullptr }; |
| 515 | status = exec_cmd(sload_argv[0], const_cast<char**>(sload_argv)); |
| 516 | if (status != 0) { |
| 517 | LOG(ERROR) << name << ": sload.f2fs failed (" << status << ") on " << location; |
| 518 | return StringValue(""); |
| 519 | } |
| 520 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 521 | return StringValue(location); |
| 522 | } else { |
| 523 | LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \"" |
| 524 | << partition_type << "\""; |
| 525 | } |
| 526 | |
| 527 | return nullptr; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 530 | Value* ShowProgressFn(const char* name, State* state, |
| 531 | const std::vector<std::unique_ptr<Expr>>& argv) { |
| 532 | if (argv.size() != 2) { |
| 533 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 534 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 535 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 536 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 537 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 538 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 539 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 540 | } |
| 541 | const std::string& frac_str = args[0]; |
| 542 | const std::string& sec_str = args[1]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 543 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 544 | double frac; |
| 545 | if (!android::base::ParseDouble(frac_str.c_str(), &frac)) { |
Tianjie Xu | 5ad8028 | 2018-01-28 15:37:48 -0800 | [diff] [blame] | 546 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name, |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 547 | frac_str.c_str()); |
| 548 | } |
| 549 | int sec; |
| 550 | if (!android::base::ParseInt(sec_str.c_str(), &sec)) { |
Tianjie Xu | 5ad8028 | 2018-01-28 15:37:48 -0800 | [diff] [blame] | 551 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name, |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 552 | sec_str.c_str()); |
| 553 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 554 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 555 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 556 | fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 557 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 558 | return StringValue(frac_str); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Tianjie Xu | 5419ad3 | 2018-02-02 16:49:15 -0800 | [diff] [blame] | 561 | Value* SetProgressFn(const char* name, State* state, |
| 562 | const std::vector<std::unique_ptr<Expr>>& argv) { |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 563 | if (argv.size() != 1) { |
| 564 | 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] | 565 | } |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 566 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 567 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 568 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 569 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 570 | } |
| 571 | const std::string& frac_str = args[0]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 572 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 573 | double frac; |
| 574 | if (!android::base::ParseDouble(frac_str.c_str(), &frac)) { |
Tianjie Xu | 5ad8028 | 2018-01-28 15:37:48 -0800 | [diff] [blame] | 575 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name, |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 576 | frac_str.c_str()); |
| 577 | } |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 578 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 579 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 580 | fprintf(ui->cmd_pipe, "set_progress %f\n", frac); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 581 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 582 | return StringValue(frac_str); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 585 | Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 586 | if (argv.size() != 1) { |
| 587 | 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] | 588 | } |
| 589 | std::string key; |
| 590 | if (!Evaluate(state, argv[0], &key)) { |
| 591 | return nullptr; |
| 592 | } |
| 593 | std::string value = android::base::GetProperty(key, ""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 594 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 595 | return StringValue(value); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 598 | // file_getprop(file, key) |
| 599 | // |
| 600 | // interprets 'file' as a getprop-style file (key=value pairs, one |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 601 | // per line. # comment lines, blank lines, lines without '=' ignored), |
Michael Runge | aa1a31e | 2014-04-25 18:47:18 -0700 | [diff] [blame] | 602 | // and returns the value for 'key' (or "" if it isn't defined). |
Tianjie Xu | 5419ad3 | 2018-02-02 16:49:15 -0800 | [diff] [blame] | 603 | Value* FileGetPropFn(const char* name, State* state, |
| 604 | const std::vector<std::unique_ptr<Expr>>& argv) { |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 605 | if (argv.size() != 2) { |
| 606 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 607 | argv.size()); |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 611 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 612 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 613 | } |
| 614 | const std::string& filename = args[0]; |
| 615 | const std::string& key = args[1]; |
| 616 | |
| 617 | struct stat st; |
| 618 | if (stat(filename.c_str(), &st) < 0) { |
| 619 | return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, |
| 620 | filename.c_str(), strerror(errno)); |
| 621 | } |
| 622 | |
| 623 | constexpr off_t MAX_FILE_GETPROP_SIZE = 65536; |
| 624 | if (st.st_size > MAX_FILE_GETPROP_SIZE) { |
| 625 | return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)", |
| 626 | filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE)); |
| 627 | } |
| 628 | |
| 629 | std::string buffer(st.st_size, '\0'); |
| 630 | unique_file f(ota_fopen(filename.c_str(), "rb")); |
| 631 | if (f == nullptr) { |
| 632 | return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename.c_str(), |
| 633 | strerror(errno)); |
| 634 | } |
| 635 | |
| 636 | if (ota_fread(&buffer[0], 1, st.st_size, f.get()) != static_cast<size_t>(st.st_size)) { |
| 637 | ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", name, |
| 638 | static_cast<size_t>(st.st_size), filename.c_str()); |
| 639 | return nullptr; |
| 640 | } |
| 641 | |
| 642 | ota_fclose(f); |
| 643 | |
| 644 | std::vector<std::string> lines = android::base::Split(buffer, "\n"); |
| 645 | for (size_t i = 0; i < lines.size(); i++) { |
| 646 | std::string line = android::base::Trim(lines[i]); |
| 647 | |
| 648 | // comment or blank line: skip to next line |
| 649 | if (line.empty() || line[0] == '#') { |
| 650 | continue; |
| 651 | } |
| 652 | size_t equal_pos = line.find('='); |
| 653 | if (equal_pos == std::string::npos) { |
| 654 | continue; |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 657 | // trim whitespace between key and '=' |
| 658 | std::string str = android::base::Trim(line.substr(0, equal_pos)); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 659 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 660 | // not the key we're looking for |
| 661 | if (key != str) continue; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 662 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 663 | return StringValue(android::base::Trim(line.substr(equal_pos + 1))); |
| 664 | } |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 665 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 666 | return StringValue(""); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 669 | // apply_patch_space(bytes) |
Tianjie Xu | 5419ad3 | 2018-02-02 16:49:15 -0800 | [diff] [blame] | 670 | Value* ApplyPatchSpaceFn(const char* name, State* state, |
| 671 | const std::vector<std::unique_ptr<Expr>>& argv) { |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 672 | if (argv.size() != 1) { |
| 673 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name, |
| 674 | argv.size()); |
| 675 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 676 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 677 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 678 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 679 | } |
| 680 | const std::string& bytes_str = args[0]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 681 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 682 | size_t bytes; |
| 683 | if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) { |
Tianjie Xu | 5ad8028 | 2018-01-28 15:37:48 -0800 | [diff] [blame] | 684 | return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name, |
| 685 | bytes_str.c_str()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 686 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 687 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 688 | // Skip the cache size check if the update is a retry. |
Tao Bao | 5ee2566 | 2018-07-11 15:55:32 -0700 | [diff] [blame] | 689 | if (state->is_retry || CheckAndFreeSpaceOnCache(bytes)) { |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 690 | return StringValue("t"); |
| 691 | } |
| 692 | return StringValue(""); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 693 | } |
| 694 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 695 | Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 696 | if (!argv.empty()) { |
| 697 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name, |
| 698 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 699 | } |
| 700 | fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n"); |
| 701 | return StringValue("t"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 704 | Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 705 | if (argv.size() < 1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 706 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name); |
| 707 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 708 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 709 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 710 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 711 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 712 | } |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 713 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 714 | char* args2[argv.size() + 1]; |
| 715 | for (size_t i = 0; i < argv.size(); i++) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 716 | args2[i] = &args[i][0]; |
| 717 | } |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 718 | args2[argv.size()] = nullptr; |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 719 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 720 | LOG(INFO) << "about to run program [" << args2[0] << "] with " << argv.size() << " args"; |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 721 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 722 | pid_t child = fork(); |
| 723 | if (child == 0) { |
| 724 | execv(args2[0], args2); |
| 725 | PLOG(ERROR) << "run_program: execv failed"; |
Tao Bao | 3da8801 | 2017-02-03 13:09:23 -0800 | [diff] [blame] | 726 | _exit(EXIT_FAILURE); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 727 | } |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 728 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 729 | int status; |
| 730 | waitpid(child, &status, 0); |
| 731 | if (WIFEXITED(status)) { |
| 732 | if (WEXITSTATUS(status) != 0) { |
| 733 | LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status); |
| 734 | } |
| 735 | } else if (WIFSIGNALED(status)) { |
| 736 | LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status); |
| 737 | } |
| 738 | |
| 739 | return StringValue(std::to_string(status)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 742 | // read_file(filename) |
Tao Bao | bafd6c7 | 2018-07-09 15:08:50 -0700 | [diff] [blame] | 743 | // 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] | 744 | Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 745 | if (argv.size() != 1) { |
| 746 | 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] | 747 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 748 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 749 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 750 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 751 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 752 | } |
| 753 | const std::string& filename = args[0]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 754 | |
Tao Bao | bafd6c7 | 2018-07-09 15:08:50 -0700 | [diff] [blame] | 755 | std::string contents; |
| 756 | if (android::base::ReadFileToString(filename, &contents)) { |
| 757 | return new Value(Value::Type::STRING, std::move(contents)); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 758 | } |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 759 | |
| 760 | // Leave it to caller to handle the failure. |
Tao Bao | bafd6c7 | 2018-07-09 15:08:50 -0700 | [diff] [blame] | 761 | PLOG(ERROR) << name << ": Failed to read " << filename; |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 762 | return StringValue(""); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 763 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 764 | |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 765 | // write_value(value, filename) |
| 766 | // Writes 'value' to 'filename'. |
| 767 | // 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] | 768 | Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 769 | if (argv.size() != 2) { |
| 770 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 771 | argv.size()); |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 775 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 776 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
| 777 | } |
| 778 | |
| 779 | const std::string& filename = args[1]; |
| 780 | if (filename.empty()) { |
| 781 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name); |
| 782 | } |
| 783 | |
| 784 | const std::string& value = args[0]; |
| 785 | if (!android::base::WriteStringToFile(value, filename)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 786 | PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\""; |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 787 | return StringValue(""); |
| 788 | } else { |
| 789 | return StringValue("t"); |
| 790 | } |
| 791 | } |
| 792 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 793 | // Immediately reboot the device. Recovery is not finished normally, |
| 794 | // so if you reboot into recovery it will re-start applying the |
| 795 | // current package (because nothing has cleared the copy of the |
| 796 | // arguments stored in the BCB). |
| 797 | // |
| 798 | // The argument is the partition name passed to the android reboot |
| 799 | // property. It can be "recovery" to boot from the recovery |
| 800 | // partition, or "" (empty string) to boot from the regular boot |
| 801 | // partition. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 802 | Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 803 | if (argv.size() != 2) { |
| 804 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 805 | argv.size()); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 806 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 807 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 808 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 809 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 810 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
| 811 | } |
| 812 | const std::string& filename = args[0]; |
| 813 | const std::string& property = args[1]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 814 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 815 | // Zero out the 'command' field of the bootloader message. Leave the rest intact. |
| 816 | bootloader_message boot; |
| 817 | std::string err; |
| 818 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 819 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 820 | return StringValue(""); |
| 821 | } |
| 822 | memset(boot.command, 0, sizeof(boot.command)); |
| 823 | if (!write_bootloader_message_to(boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 824 | LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 825 | return StringValue(""); |
| 826 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 827 | |
Tao Bao | 2c52639 | 2018-05-03 23:01:13 -0700 | [diff] [blame] | 828 | reboot("reboot," + property); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 829 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 830 | sleep(5); |
| 831 | return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | // Store a string value somewhere that future invocations of recovery |
| 835 | // can access it. This value is called the "stage" and can be used to |
| 836 | // drive packages that need to do reboots in the middle of |
| 837 | // installation and keep track of where they are in the multi-stage |
| 838 | // install. |
| 839 | // |
| 840 | // The first argument is the block device for the misc partition |
| 841 | // ("/misc" in the fstab), which is where this value is stored. The |
| 842 | // second argument is the string to store; it should not exceed 31 |
| 843 | // bytes. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 844 | Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 845 | if (argv.size() != 2) { |
| 846 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 847 | argv.size()); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 848 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 849 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 850 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 851 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 852 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 853 | } |
| 854 | const std::string& filename = args[0]; |
| 855 | const std::string& stagestr = args[1]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 856 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 857 | // Store this value in the misc partition, immediately after the |
| 858 | // bootloader message that the main recovery uses to save its |
| 859 | // arguments in case of the device restarting midway through |
| 860 | // package installation. |
| 861 | bootloader_message boot; |
| 862 | std::string err; |
| 863 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 864 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 865 | return StringValue(""); |
| 866 | } |
| 867 | strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage)); |
| 868 | if (!write_bootloader_message_to(boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 869 | LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 870 | return StringValue(""); |
| 871 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 872 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 873 | return StringValue(filename); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | // Return the value most recently saved with SetStageFn. The argument |
| 877 | // is the block device for the misc partition. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 878 | Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 879 | if (argv.size() != 1) { |
| 880 | 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] | 881 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 882 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 883 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 884 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 885 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 886 | } |
| 887 | const std::string& filename = args[0]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 888 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 889 | bootloader_message boot; |
| 890 | std::string err; |
| 891 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 892 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 893 | return StringValue(""); |
| 894 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 895 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 896 | return StringValue(boot.stage); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 897 | } |
| 898 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 899 | Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 900 | if (argv.size() != 2) { |
| 901 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 902 | argv.size()); |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 903 | } |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 904 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 905 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 906 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 907 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 908 | } |
| 909 | const std::string& filename = args[0]; |
| 910 | const std::string& len_str = args[1]; |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 911 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 912 | size_t len; |
| 913 | if (!android::base::ParseUint(len_str.c_str(), &len)) { |
| 914 | return nullptr; |
| 915 | } |
| 916 | unique_fd fd(ota_open(filename.c_str(), O_WRONLY, 0644)); |
| 917 | // The wipe_block_device function in ext4_utils returns 0 on success and 1 |
| 918 | // for failure. |
| 919 | int status = wipe_block_device(fd, len); |
| 920 | return StringValue((status == 0) ? "t" : ""); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 921 | } |
| 922 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 923 | Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 924 | if (!argv.empty()) { |
| 925 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name, |
| 926 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 927 | } |
| 928 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 929 | fprintf(ui->cmd_pipe, "enable_reboot\n"); |
| 930 | return StringValue("t"); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 933 | Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 934 | if (argv.empty()) { |
| 935 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 936 | } |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 937 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 938 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 939 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 940 | return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name); |
| 941 | } |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 942 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 943 | char* args2[argv.size() + 1]; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 944 | // Tune2fs expects the program name as its args[0] |
| 945 | args2[0] = const_cast<char*>(name); |
| 946 | if (args2[0] == nullptr) { |
| 947 | return nullptr; |
| 948 | } |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 949 | for (size_t i = 0; i < argv.size(); ++i) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 950 | args2[i + 1] = &args[i][0]; |
| 951 | } |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 952 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 953 | // tune2fs changes the file system parameters on an ext2 file system; it |
| 954 | // returns 0 on success. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 955 | int result = tune2fs_main(argv.size() + 1, args2); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 956 | if (result != 0) { |
| 957 | return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result); |
| 958 | } |
| 959 | return StringValue("t"); |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 960 | } |
| 961 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 962 | void RegisterInstallFunctions() { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 963 | RegisterFunction("mount", MountFn); |
| 964 | RegisterFunction("is_mounted", IsMountedFn); |
| 965 | RegisterFunction("unmount", UnmountFn); |
| 966 | RegisterFunction("format", FormatFn); |
| 967 | RegisterFunction("show_progress", ShowProgressFn); |
| 968 | RegisterFunction("set_progress", SetProgressFn); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 969 | RegisterFunction("package_extract_file", PackageExtractFileFn); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 970 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 971 | RegisterFunction("getprop", GetPropFn); |
| 972 | RegisterFunction("file_getprop", FileGetPropFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 973 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 974 | RegisterFunction("apply_patch", ApplyPatchFn); |
| 975 | RegisterFunction("apply_patch_check", ApplyPatchCheckFn); |
| 976 | RegisterFunction("apply_patch_space", ApplyPatchSpaceFn); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 977 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 978 | RegisterFunction("wipe_block_device", WipeBlockDeviceFn); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 979 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 980 | RegisterFunction("read_file", ReadFileFn); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 981 | RegisterFunction("write_value", WriteValueFn); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 982 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 983 | RegisterFunction("wipe_cache", WipeCacheFn); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 984 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 985 | RegisterFunction("ui_print", UIPrintFn); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 986 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 987 | RegisterFunction("run_program", RunProgramFn); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 988 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 989 | RegisterFunction("reboot_now", RebootNowFn); |
| 990 | RegisterFunction("get_stage", GetStageFn); |
| 991 | RegisterFunction("set_stage", SetStageFn); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 992 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 993 | RegisterFunction("enable_reboot", EnableRebootFn); |
| 994 | RegisterFunction("tune2fs", Tune2FsFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 995 | } |