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" |
| 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) { |
| 197 | uiPrintf(state, "%s: failed to mount %s at %s: %s\n", name, location.c_str(), |
| 198 | mount_point.c_str(), strerror(errno)); |
| 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) { |
| 247 | uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point.c_str()); |
| 248 | return nullptr; |
| 249 | } else { |
| 250 | int ret = unmount_mounted_volume(vol); |
| 251 | if (ret != 0) { |
| 252 | uiPrintf(state, "unmount of %s failed (%d): %s\n", mount_point.c_str(), ret, 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") { |
| 319 | int status = make_ext4fs(location.c_str(), size, mount_point.c_str(), sehandle); |
| 320 | if (status != 0) { |
| 321 | LOG(ERROR) << name << ": make_ext4fs failed (" << status << ") on " << location; |
| 322 | return StringValue(""); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 323 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 324 | return StringValue(location); |
| 325 | } else if (fs_type == "f2fs") { |
| 326 | if (size < 0) { |
| 327 | LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size; |
| 328 | return StringValue(""); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 329 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 330 | std::string num_sectors = std::to_string(size / 512); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 331 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 332 | const char* f2fs_path = "/sbin/mkfs.f2fs"; |
| 333 | const char* const f2fs_argv[] = { "mkfs.f2fs", "-t", "-d1", location.c_str(), |
| 334 | num_sectors.c_str(), nullptr }; |
| 335 | int status = exec_cmd(f2fs_path, const_cast<char* const*>(f2fs_argv)); |
| 336 | if (status != 0) { |
| 337 | LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location; |
| 338 | return StringValue(""); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 339 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 340 | return StringValue(location); |
| 341 | } else { |
| 342 | LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \"" |
| 343 | << partition_type << "\""; |
| 344 | } |
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 nullptr; |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 347 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 348 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 349 | Value* ShowProgressFn(const char* name, State* state, |
| 350 | const std::vector<std::unique_ptr<Expr>>& argv) { |
| 351 | if (argv.size() != 2) { |
| 352 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 353 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 354 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 355 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 356 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 357 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 358 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 359 | } |
| 360 | const std::string& frac_str = args[0]; |
| 361 | const std::string& sec_str = args[1]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 362 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 363 | double frac; |
| 364 | if (!android::base::ParseDouble(frac_str.c_str(), &frac)) { |
| 365 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name, |
| 366 | frac_str.c_str()); |
| 367 | } |
| 368 | int sec; |
| 369 | if (!android::base::ParseInt(sec_str.c_str(), &sec)) { |
| 370 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name, |
| 371 | sec_str.c_str()); |
| 372 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 373 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 374 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 375 | fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 376 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 377 | return StringValue(frac_str); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 380 | Value* SetProgressFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 381 | if (argv.size() != 1) { |
| 382 | 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] | 383 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 384 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 385 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 386 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 387 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 388 | } |
| 389 | const std::string& frac_str = args[0]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 390 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 391 | double frac; |
| 392 | if (!android::base::ParseDouble(frac_str.c_str(), &frac)) { |
| 393 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name, |
| 394 | frac_str.c_str()); |
| 395 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 396 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 397 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 398 | fprintf(ui->cmd_pipe, "set_progress %f\n", frac); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 399 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 400 | return StringValue(frac_str); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 403 | // package_extract_dir(package_dir, dest_dir) |
| 404 | // Extracts all files from the package underneath package_dir and writes them to the |
| 405 | // corresponding tree beneath dest_dir. Any existing files are overwritten. |
| 406 | // Example: package_extract_dir("system", "/system") |
| 407 | // |
| 408 | // Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 409 | Value* PackageExtractDirFn(const char* name, State* state, |
| 410 | const std::vector<std::unique_ptr<Expr>>&argv) { |
| 411 | if (argv.size() != 2) { |
| 412 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 413 | argv.size()); |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 414 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 415 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 416 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 417 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 418 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 419 | } |
| 420 | const std::string& zip_path = args[0]; |
| 421 | const std::string& dest_path = args[1]; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 422 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 423 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 424 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 425 | // To create a consistent system image, never use the clock for timestamps. |
| 426 | constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 427 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 428 | bool success = ExtractPackageRecursive(za, zip_path, dest_path, ×tamp, sehandle); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 429 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 430 | return StringValue(success ? "t" : ""); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 433 | // package_extract_file(package_file[, dest_file]) |
| 434 | // Extracts a single package_file from the update package and writes it to dest_file, |
| 435 | // overwriting existing files if necessary. Without the dest_file argument, returns the |
| 436 | // contents of the package file as a binary blob. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 437 | Value* PackageExtractFileFn(const char* name, State* state, |
| 438 | const std::vector<std::unique_ptr<Expr>>& argv) { |
| 439 | if (argv.size() < 1 || argv.size() > 2) { |
| 440 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name, |
| 441 | argv.size()); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 442 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 443 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 444 | if (argv.size() == 2) { |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 445 | // The two-argument version extracts to a file. |
| 446 | |
| 447 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 448 | if (!ReadArgs(state, argv, &args)) { |
| 449 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name, |
| 450 | argv.size()); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 451 | } |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 452 | const std::string& zip_path = args[0]; |
| 453 | const std::string& dest_path = args[1]; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 454 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 455 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
| 456 | ZipString zip_string_path(zip_path.c_str()); |
| 457 | ZipEntry entry; |
| 458 | if (FindEntry(za, zip_string_path, &entry) != 0) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 459 | LOG(ERROR) << name << ": no " << zip_path << " in package"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 460 | return StringValue(""); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 461 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 462 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 463 | unique_fd fd(TEMP_FAILURE_RETRY( |
| 464 | 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] | 465 | if (fd == -1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 466 | PLOG(ERROR) << name << ": can't open " << dest_path << " for write"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 467 | return StringValue(""); |
| 468 | } |
| 469 | |
| 470 | bool success = true; |
| 471 | int32_t ret = ExtractEntryToFile(za, &entry, fd); |
| 472 | if (ret != 0) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 473 | LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" (" |
| 474 | << entry.uncompressed_length << " bytes) to \"" << dest_path |
| 475 | << "\": " << ErrorCodeString(ret); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 476 | success = false; |
| 477 | } |
| 478 | if (ota_fsync(fd) == -1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 479 | PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 480 | success = false; |
| 481 | } |
| 482 | if (ota_close(fd) == -1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 483 | PLOG(ERROR) << "close of \"" << dest_path << "\" failed"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 484 | success = false; |
| 485 | } |
| 486 | |
| 487 | return StringValue(success ? "t" : ""); |
| 488 | } else { |
| 489 | // The one-argument version returns the contents of the file as the result. |
| 490 | |
| 491 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 492 | if (!ReadArgs(state, argv, &args)) { |
| 493 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name, |
| 494 | argv.size()); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 495 | } |
| 496 | const std::string& zip_path = args[0]; |
| 497 | |
| 498 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
| 499 | ZipString zip_string_path(zip_path.c_str()); |
| 500 | ZipEntry entry; |
| 501 | if (FindEntry(za, zip_string_path, &entry) != 0) { |
| 502 | return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name, |
| 503 | zip_path.c_str()); |
| 504 | } |
| 505 | |
| 506 | std::string buffer; |
| 507 | buffer.resize(entry.uncompressed_length); |
| 508 | |
| 509 | int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size()); |
| 510 | if (ret != 0) { |
| 511 | return ErrorAbort(state, kPackageExtractFileFailure, |
| 512 | "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name, |
| 513 | zip_path.c_str(), buffer.size(), ErrorCodeString(ret)); |
| 514 | } |
| 515 | |
| 516 | return new Value(VAL_BLOB, buffer); |
| 517 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 520 | Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 521 | if (argv.size() != 1) { |
| 522 | 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] | 523 | } |
| 524 | std::string key; |
| 525 | if (!Evaluate(state, argv[0], &key)) { |
| 526 | return nullptr; |
| 527 | } |
| 528 | std::string value = android::base::GetProperty(key, ""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 529 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 530 | return StringValue(value); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 533 | // file_getprop(file, key) |
| 534 | // |
| 535 | // interprets 'file' as a getprop-style file (key=value pairs, one |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 536 | // per line. # comment lines, blank lines, lines without '=' ignored), |
Michael Runge | aa1a31e | 2014-04-25 18:47:18 -0700 | [diff] [blame] | 537 | // and returns the value for 'key' (or "" if it isn't defined). |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 538 | Value* FileGetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 539 | if (argv.size() != 2) { |
| 540 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 541 | argv.size()); |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 545 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 546 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 547 | } |
| 548 | const std::string& filename = args[0]; |
| 549 | const std::string& key = args[1]; |
| 550 | |
| 551 | struct stat st; |
| 552 | if (stat(filename.c_str(), &st) < 0) { |
| 553 | return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, |
| 554 | filename.c_str(), strerror(errno)); |
| 555 | } |
| 556 | |
| 557 | constexpr off_t MAX_FILE_GETPROP_SIZE = 65536; |
| 558 | if (st.st_size > MAX_FILE_GETPROP_SIZE) { |
| 559 | return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)", |
| 560 | filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE)); |
| 561 | } |
| 562 | |
| 563 | std::string buffer(st.st_size, '\0'); |
| 564 | unique_file f(ota_fopen(filename.c_str(), "rb")); |
| 565 | if (f == nullptr) { |
| 566 | return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename.c_str(), |
| 567 | strerror(errno)); |
| 568 | } |
| 569 | |
| 570 | if (ota_fread(&buffer[0], 1, st.st_size, f.get()) != static_cast<size_t>(st.st_size)) { |
| 571 | ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", name, |
| 572 | static_cast<size_t>(st.st_size), filename.c_str()); |
| 573 | return nullptr; |
| 574 | } |
| 575 | |
| 576 | ota_fclose(f); |
| 577 | |
| 578 | std::vector<std::string> lines = android::base::Split(buffer, "\n"); |
| 579 | for (size_t i = 0; i < lines.size(); i++) { |
| 580 | std::string line = android::base::Trim(lines[i]); |
| 581 | |
| 582 | // comment or blank line: skip to next line |
| 583 | if (line.empty() || line[0] == '#') { |
| 584 | continue; |
| 585 | } |
| 586 | size_t equal_pos = line.find('='); |
| 587 | if (equal_pos == std::string::npos) { |
| 588 | continue; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 591 | // trim whitespace between key and '=' |
| 592 | std::string str = android::base::Trim(line.substr(0, equal_pos)); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 593 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 594 | // not the key we're looking for |
| 595 | if (key != str) continue; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 596 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 597 | return StringValue(android::base::Trim(line.substr(equal_pos + 1))); |
| 598 | } |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 599 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 600 | return StringValue(""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 603 | // apply_patch_space(bytes) |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 604 | Value* ApplyPatchSpaceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 605 | if (argv.size() != 1) { |
| 606 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name, |
| 607 | argv.size()); |
| 608 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 609 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 610 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 611 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 612 | } |
| 613 | const std::string& bytes_str = args[0]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 614 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 615 | size_t bytes; |
| 616 | if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) { |
| 617 | return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n", |
| 618 | name, bytes_str.c_str()); |
| 619 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 620 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 621 | return StringValue(CacheSizeCheck(bytes) ? "" : "t"); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 624 | // apply_patch(src_file, tgt_file, tgt_sha1, tgt_size, patch1_sha1, patch1_blob, [...]) |
| 625 | // Applies a binary patch to the src_file to produce the tgt_file. If the desired target is the |
| 626 | // same as the source, pass "-" for tgt_file. tgt_sha1 and tgt_size are the expected final SHA1 |
| 627 | // hash and size of the target file. The remaining arguments must come in pairs: a SHA1 hash (a |
| 628 | // 40-character hex string) and a blob. The blob is the patch to be applied when the source |
| 629 | // file's current contents have the given SHA1. |
| 630 | // |
| 631 | // The patching is done in a safe manner that guarantees the target file either has the desired |
| 632 | // SHA1 hash and size, or it is untouched -- it will not be left in an unrecoverable intermediate |
| 633 | // state. If the process is interrupted during patching, the target file may be in an intermediate |
| 634 | // state; a copy exists in the cache partition so restarting the update can successfully update |
| 635 | // the file. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 636 | Value* ApplyPatchFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 637 | if (argv.size() < 6 || (argv.size() % 2) == 1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 638 | return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an " |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 639 | "even number, got %zu", name, argv.size()); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 642 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 643 | if (!ReadArgs(state, argv, &args, 0, 4)) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 644 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 645 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 646 | const std::string& source_filename = args[0]; |
| 647 | const std::string& target_filename = args[1]; |
| 648 | const std::string& target_sha1 = args[2]; |
| 649 | const std::string& target_size_str = args[3]; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 650 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 651 | size_t target_size; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 652 | if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) { |
| 653 | return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", |
| 654 | name, target_size_str.c_str()); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 657 | int patchcount = (argv.size()-4) / 2; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 658 | std::vector<std::unique_ptr<Value>> arg_values; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 659 | if (!ReadValueArgs(state, argv, &arg_values, 4, argv.size() - 4)) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 660 | return nullptr; |
| 661 | } |
| 662 | |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 663 | for (int i = 0; i < patchcount; ++i) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 664 | if (arg_values[i * 2]->type != VAL_STRING) { |
| 665 | return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, |
| 666 | i * 2); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 667 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 668 | if (arg_values[i * 2 + 1]->type != VAL_BLOB) { |
| 669 | return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, |
| 670 | i * 2 + 1); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 671 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 672 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 673 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 674 | std::vector<std::string> patch_sha_str; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 675 | std::vector<std::unique_ptr<Value>> patches; |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 676 | for (int i = 0; i < patchcount; ++i) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 677 | patch_sha_str.push_back(arg_values[i * 2]->data); |
| 678 | patches.push_back(std::move(arg_values[i * 2 + 1])); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 679 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 680 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 681 | int result = applypatch(source_filename.c_str(), target_filename.c_str(), |
| 682 | target_sha1.c_str(), target_size, |
| 683 | patch_sha_str, patches, nullptr); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 684 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 685 | return StringValue(result == 0 ? "t" : ""); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 686 | } |
| 687 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 688 | // apply_patch_check(filename, [sha1, ...]) |
| 689 | // Returns true if the contents of filename or the temporary copy in the cache partition (if |
| 690 | // present) have a SHA-1 checksum equal to one of the given sha1 values. sha1 values are |
| 691 | // specified as 40 hex digits. This function differs from sha1_check(read_file(filename), |
| 692 | // sha1 [, ...]) in that it knows to check the cache partition copy, so apply_patch_check() will |
| 693 | // 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] | 694 | Value* ApplyPatchCheckFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 695 | if (argv.size() < 1) { |
| 696 | return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %zu", name, |
| 697 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 698 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 699 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 700 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 701 | if (!ReadArgs(state, argv, &args, 0, 1)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 702 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 703 | } |
| 704 | const std::string& filename = args[0]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 705 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 706 | std::vector<std::string> sha1s; |
Tao Bao | db56eb0 | 2017-03-23 06:34:20 -0700 | [diff] [blame] | 707 | if (argv.size() > 1 && !ReadArgs(state, argv, &sha1s, 1, argv.size() - 1)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 708 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 709 | } |
| 710 | int result = applypatch_check(filename.c_str(), sha1s); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 711 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 712 | return StringValue(result == 0 ? "t" : ""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 715 | // This is the updater side handler for ui_print() in edify script. Contents |
| 716 | // will be sent over to the recovery side for on-screen display. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 717 | 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] | 718 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 719 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 720 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 721 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 722 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 723 | std::string buffer = android::base::Join(args, "") + "\n"; |
| 724 | uiPrint(state, buffer); |
| 725 | return StringValue(buffer); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 728 | Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 729 | if (!argv.empty()) { |
| 730 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name, |
| 731 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 732 | } |
| 733 | fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n"); |
| 734 | return StringValue("t"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 737 | Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 738 | if (argv.size() < 1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 739 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name); |
| 740 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 741 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 742 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 743 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 744 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 745 | } |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 746 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 747 | char* args2[argv.size() + 1]; |
| 748 | for (size_t i = 0; i < argv.size(); i++) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 749 | args2[i] = &args[i][0]; |
| 750 | } |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 751 | args2[argv.size()] = nullptr; |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 752 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 753 | LOG(INFO) << "about to run program [" << args2[0] << "] with " << argv.size() << " args"; |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 754 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 755 | pid_t child = fork(); |
| 756 | if (child == 0) { |
| 757 | execv(args2[0], args2); |
| 758 | PLOG(ERROR) << "run_program: execv failed"; |
Tao Bao | 3da8801 | 2017-02-03 13:09:23 -0800 | [diff] [blame] | 759 | _exit(EXIT_FAILURE); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 760 | } |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 761 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 762 | int status; |
| 763 | waitpid(child, &status, 0); |
| 764 | if (WIFEXITED(status)) { |
| 765 | if (WEXITSTATUS(status) != 0) { |
| 766 | LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 767 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 768 | } else if (WIFSIGNALED(status)) { |
| 769 | LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status); |
| 770 | } |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 771 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 772 | return StringValue(std::to_string(status)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 775 | // sha1_check(data) |
| 776 | // to return the sha1 of the data (given in the format returned by |
| 777 | // read_file). |
| 778 | // |
| 779 | // sha1_check(data, sha1_hex, [sha1_hex, ...]) |
| 780 | // returns the sha1 of the file if it matches any of the hex |
| 781 | // strings passed, or "" if it does not equal any of them. |
| 782 | // |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 783 | Value* Sha1CheckFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 784 | if (argv.size() < 1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 785 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name); |
| 786 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 787 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 788 | std::vector<std::unique_ptr<Value>> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 789 | if (!ReadValueArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 790 | return nullptr; |
| 791 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 792 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 793 | if (args[0]->type == VAL_INVALID) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 794 | return StringValue(""); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 795 | } |
| 796 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 797 | 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] | 798 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 799 | if (argv.size() == 1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 800 | return StringValue(print_sha1(digest)); |
| 801 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 802 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 803 | for (size_t i = 1; i < argv.size(); ++i) { |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 804 | uint8_t arg_digest[SHA_DIGEST_LENGTH]; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 805 | if (args[i]->type != VAL_STRING) { |
| 806 | LOG(ERROR) << name << "(): arg " << i << " is not a string; skipping"; |
| 807 | } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) { |
| 808 | // Warn about bad args and skip them. |
| 809 | LOG(ERROR) << name << "(): error parsing \"" << args[i]->data << "\" as sha-1; skipping"; |
| 810 | } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) { |
| 811 | // Found a match. |
| 812 | return args[i].release(); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 813 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | // Didn't match any of the hex strings; return false. |
| 817 | return StringValue(""); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 818 | } |
| 819 | |
Hristo Bojinov | db314d6 | 2010-08-02 10:29:49 -0700 | [diff] [blame] | 820 | // Read a local file and return its contents (the Value* returned |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 821 | // is actually a FileContents*). |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 822 | Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 823 | if (argv.size() != 1) { |
| 824 | 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] | 825 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 826 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 827 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 828 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 829 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 830 | } |
| 831 | const std::string& filename = args[0]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 832 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 833 | Value* v = new Value(VAL_INVALID, ""); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 834 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 835 | FileContents fc; |
| 836 | if (LoadFileContents(filename.c_str(), &fc) == 0) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 837 | v->type = VAL_BLOB; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 838 | v->data = std::string(fc.data.begin(), fc.data.end()); |
| 839 | } |
| 840 | return v; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 841 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 842 | |
Tao Bao | 1bf1772 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 843 | // write_value(value, filename) |
| 844 | // Writes 'value' to 'filename'. |
| 845 | // 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] | 846 | Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 847 | if (argv.size() != 2) { |
| 848 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 849 | argv.size()); |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 850 | } |
Tao Bao | 1bf1772 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 851 | |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 852 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 853 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 854 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
| 855 | } |
Tao Bao | 1bf1772 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 856 | |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 857 | const std::string& filename = args[1]; |
| 858 | if (filename.empty()) { |
| 859 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name); |
| 860 | } |
Tao Bao | 1bf1772 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 861 | |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 862 | const std::string& value = args[0]; |
| 863 | if (!android::base::WriteStringToFile(value, filename)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 864 | PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\""; |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 865 | return StringValue(""); |
| 866 | } else { |
| 867 | return StringValue("t"); |
| 868 | } |
Tao Bao | 1bf1772 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 871 | // Immediately reboot the device. Recovery is not finished normally, |
| 872 | // so if you reboot into recovery it will re-start applying the |
| 873 | // current package (because nothing has cleared the copy of the |
| 874 | // arguments stored in the BCB). |
| 875 | // |
| 876 | // The argument is the partition name passed to the android reboot |
| 877 | // property. It can be "recovery" to boot from the recovery |
| 878 | // partition, or "" (empty string) to boot from the regular boot |
| 879 | // partition. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 880 | Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 881 | if (argv.size() != 2) { |
| 882 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 883 | argv.size()); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 884 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 885 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 886 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 887 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 888 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
| 889 | } |
| 890 | const std::string& filename = args[0]; |
| 891 | const std::string& property = args[1]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 892 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 893 | // Zero out the 'command' field of the bootloader message. Leave the rest intact. |
| 894 | bootloader_message boot; |
| 895 | std::string err; |
| 896 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 897 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 898 | return StringValue(""); |
| 899 | } |
| 900 | memset(boot.command, 0, sizeof(boot.command)); |
| 901 | if (!write_bootloader_message_to(boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 902 | LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 903 | return StringValue(""); |
| 904 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 905 | |
Dmitri Plotnikov | ed9db0f | 2017-04-18 08:28:26 -0700 | [diff] [blame] | 906 | std::string reboot_cmd = "reboot," + property; |
| 907 | if (android::base::GetBoolProperty("ro.boot.quiescent", false)) { |
| 908 | reboot_cmd += ",quiescent"; |
| 909 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 910 | android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 911 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 912 | sleep(5); |
| 913 | return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | // Store a string value somewhere that future invocations of recovery |
| 917 | // can access it. This value is called the "stage" and can be used to |
| 918 | // drive packages that need to do reboots in the middle of |
| 919 | // installation and keep track of where they are in the multi-stage |
| 920 | // install. |
| 921 | // |
| 922 | // The first argument is the block device for the misc partition |
| 923 | // ("/misc" in the fstab), which is where this value is stored. The |
| 924 | // second argument is the string to store; it should not exceed 31 |
| 925 | // bytes. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 926 | Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 927 | if (argv.size() != 2) { |
| 928 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 929 | argv.size()); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 930 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 931 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 932 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 933 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 934 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 935 | } |
| 936 | const std::string& filename = args[0]; |
| 937 | const std::string& stagestr = args[1]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 938 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 939 | // Store this value in the misc partition, immediately after the |
| 940 | // bootloader message that the main recovery uses to save its |
| 941 | // arguments in case of the device restarting midway through |
| 942 | // package installation. |
| 943 | bootloader_message boot; |
| 944 | std::string err; |
| 945 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 946 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 947 | return StringValue(""); |
| 948 | } |
| 949 | strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage)); |
| 950 | if (!write_bootloader_message_to(boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 951 | LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 952 | return StringValue(""); |
| 953 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 954 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 955 | return StringValue(filename); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | // Return the value most recently saved with SetStageFn. The argument |
| 959 | // is the block device for the misc partition. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 960 | Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 961 | if (argv.size() != 1) { |
| 962 | 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] | 963 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 964 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 965 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 966 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 967 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 968 | } |
| 969 | const std::string& filename = args[0]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 970 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 971 | bootloader_message boot; |
| 972 | std::string err; |
| 973 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 974 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 975 | return StringValue(""); |
| 976 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 977 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 978 | return StringValue(boot.stage); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 979 | } |
| 980 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 981 | Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 982 | if (argv.size() != 2) { |
| 983 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name, |
| 984 | argv.size()); |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 985 | } |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 986 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 987 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 988 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 989 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 990 | } |
| 991 | const std::string& filename = args[0]; |
| 992 | const std::string& len_str = args[1]; |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 993 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 994 | size_t len; |
| 995 | if (!android::base::ParseUint(len_str.c_str(), &len)) { |
| 996 | return nullptr; |
| 997 | } |
| 998 | unique_fd fd(ota_open(filename.c_str(), O_WRONLY, 0644)); |
| 999 | // The wipe_block_device function in ext4_utils returns 0 on success and 1 |
| 1000 | // for failure. |
| 1001 | int status = wipe_block_device(fd, len); |
| 1002 | return StringValue((status == 0) ? "t" : ""); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 1005 | Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 1006 | if (!argv.empty()) { |
| 1007 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name, |
| 1008 | argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1009 | } |
| 1010 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 1011 | fprintf(ui->cmd_pipe, "enable_reboot\n"); |
| 1012 | return StringValue("t"); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 1015 | 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] | 1016 | #ifdef HAVE_LIBTUNE2FS |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 1017 | if (argv.empty()) { |
| 1018 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1019 | } |
Michael Runge | b278c25 | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1020 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1021 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 1022 | if (!ReadArgs(state, argv, &args)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1023 | return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name); |
| 1024 | } |
Michael Runge | b278c25 | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1025 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 1026 | char* args2[argv.size() + 1]; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1027 | // Tune2fs expects the program name as its args[0] |
| 1028 | args2[0] = const_cast<char*>(name); |
| 1029 | if (args2[0] == nullptr) { |
| 1030 | return nullptr; |
| 1031 | } |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 1032 | for (size_t i = 0; i < argv.size(); ++i) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1033 | args2[i + 1] = &args[i][0]; |
| 1034 | } |
Michael Runge | b278c25 | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1035 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1036 | // tune2fs changes the file system parameters on an ext2 file system; it |
| 1037 | // returns 0 on success. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 1038 | int result = tune2fs_main(argv.size() + 1, args2); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1039 | if (result != 0) { |
| 1040 | return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result); |
| 1041 | } |
| 1042 | return StringValue("t"); |
Ethan Yonker | 7e1b986 | 2015-03-19 14:10:01 -0500 | [diff] [blame] | 1043 | #else |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 1044 | return ErrorAbort(state, kTune2FsFailure, "%s() support not present, no libtune2fs", name); |
Ethan Yonker | 7e1b986 | 2015-03-19 14:10:01 -0500 | [diff] [blame] | 1045 | #endif // HAVE_LIBTUNE2FS |
Michael Runge | b278c25 | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1046 | } |
| 1047 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1048 | void RegisterInstallFunctions() { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1049 | RegisterFunction("mount", MountFn); |
| 1050 | RegisterFunction("is_mounted", IsMountedFn); |
| 1051 | RegisterFunction("unmount", UnmountFn); |
| 1052 | RegisterFunction("format", FormatFn); |
| 1053 | RegisterFunction("show_progress", ShowProgressFn); |
| 1054 | RegisterFunction("set_progress", SetProgressFn); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1055 | RegisterFunction("package_extract_dir", PackageExtractDirFn); |
| 1056 | RegisterFunction("package_extract_file", PackageExtractFileFn); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1057 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1058 | RegisterFunction("getprop", GetPropFn); |
| 1059 | RegisterFunction("file_getprop", FileGetPropFn); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1060 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1061 | RegisterFunction("apply_patch", ApplyPatchFn); |
| 1062 | RegisterFunction("apply_patch_check", ApplyPatchCheckFn); |
| 1063 | RegisterFunction("apply_patch_space", ApplyPatchSpaceFn); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1064 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1065 | RegisterFunction("wipe_block_device", WipeBlockDeviceFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1066 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1067 | RegisterFunction("read_file", ReadFileFn); |
| 1068 | RegisterFunction("sha1_check", Sha1CheckFn); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1069 | RegisterFunction("write_value", WriteValueFn); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1070 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1071 | RegisterFunction("wipe_cache", WipeCacheFn); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1072 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1073 | RegisterFunction("ui_print", UIPrintFn); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1074 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1075 | RegisterFunction("run_program", RunProgramFn); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1076 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1077 | RegisterFunction("reboot_now", RebootNowFn); |
| 1078 | RegisterFunction("get_stage", GetStageFn); |
| 1079 | RegisterFunction("set_stage", SetStageFn); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1080 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1081 | RegisterFunction("enable_reboot", EnableRebootFn); |
| 1082 | RegisterFunction("tune2fs", Tune2FsFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1083 | } |