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